2014年6月17日 星期二

java-public ,java-static


public

  • public means that the method is visible and can be called from other objects of other types. 
  • Other alternatives are private, protected, package and package-private.  
  • The public keyword is an access specifier, which allows the programmer to control the visibility of class members. 
  • When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. 
  • (The opposite of public is private, which prevents a member from being used by code defined outside of its class.)
  • First public means that any other object can access it.
  • Public - means that the class (program) is available for use by any other class.
  • public means you can access the class from anywhere in the class/object or outside of the package or class
  • Considering the typical top-level class. Only public and no modifier access modifiers may be used at the top level so you'll either see public or you won't see any access modifier at all.


static

  • static means constant in which block of statement used only 1 time
  • static means that the method is associated with the class, not a specific instance (object) of that class.
  • This means that you can call a static method without creating an object of the class.
  • It doesn't have any object state, so you can call it without instantiating an object
  • Static - creates a class. Can also be applied to variables and methods,making them class methods/variables instead of just local to a particular instance of the class.
  • static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class. Because of use of a static keyword main() is your first method to be invoked.. static doesn't need to any object to instance... so,main( ) is called by the Java interpreter before any objects are made.
  • `static`` is used because you may not have a need to create an actual object at the top level (but sometimes you will want to so you may not always see/use static. There are other reasons why you wouldn't include static too but this is the typical one at the top level.)


void


  • void means that the method has no return value. If the method returned an int you would write intinstead of void.
  • void is used because usually you're not going to be returning a value from the top level (class). (sometimes you'll want to return a value other than NULL so void may not always be used either especially in the case when you have declared, initialized an object at the top level that you are assigning some value to).



main

  • In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started.

  • The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.

  • The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.

沒有留言:

張貼留言