|Access||Adobe photoshop||Algoritmi||Autocad||Baze de date||C||C sharp|
|Calculatoare||Corel draw||Dot net||Excel||Fox pro||Frontpage||Hardware|
|Php||Power point||Retele calculatoare||Sql||Tutorials||Webdesign||Windows|
|Asp||Autocad||C||Dot net||Excel||Fox pro||Html||Java|
In Java, the access specifiers can also be used to determine which classes within a library will be available to the users of that library. If you want a class to be available to a client programmer, you place the public keyword somewhere before the opening brace of the class body. This controls whether the client programmer can even create an object of the class.
To control the access of a class, the specifier must appear before the keyword class. Thus you can say:
Up to now, most of the methods have been returning either void or a primitive type so the definition:
might look a little confusing at first. The word before the method name (access) tells what the method returns. So far this has most often been void, which means it returns nothing. But you can also return a handle to an object, which is what happens here. This method returns a handle to an object of class Soup.
The class Soup shows how to prevent direct creation of a class by making all the constructors private. Remember that if you donít explicitly create at least one constructor, the default constructor (a constructor with no arguments) will be created for you. By writing the default constructor, it wonít be created automatically. By making it private, no one can create an object of that class. But now how does anyone use this class? The above example shows two options. First, a static method is created that creates a new Soup and returns a handle to it. This could be useful if you want to do some extra operations on the Soup before returning it, or if you want to keep count of how many Soup objects to create (perhaps to restrict their population).
The second option uses whatís called a design pattern, which will be discussed later in this book. This particular pattern is called a ďsingletonĒ because it allows only a single object to ever be created. The object of class Soup is created as a static private member of Soup, so thereís one and only one, and you canít get at it except through the public method access( ).
As previously mentioned, if you donít put an access specifier for class access it defaults to ďfriendly.Ē This means that an object of that class can be created by any other class in the package, but not outside the package. (Remember, all the files within the same directory that donít have explicit package declarations are implicitly part of the default package for that directory.) However, if a static member of that class is public, the client programmer can still access that static member even though they cannot create an object of that class.
Politica de confidentialitate|
Adauga cod HTML in site