Advertisement
Felanpro

static members

Aug 27th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. class program1
  2. {  
  3.     public void main(String[] args)
  4.     {
  5.          Abc.method();
  6.          Abc.i = 12;
  7.     }
  8. }
  9.  
  10. class Abc
  11. {
  12.     /*A static member belongs to the class and not a specific instance/object.*/
  13.     //a static variable is shared among all the objects/instances of a class.
  14.     //If I don't want to instantiate/create an object for a class I can just use static and just have to type the class name.
  15.     //You can't have a non static variable in a static method.
  16.    
  17.    
  18.     static int i; //Static tells the program that you only need class name to call.
  19.    
  20.     public static void method() //static tells the program that you don't need an object to call the method from the class only class name
  21.     {
  22.         System.out.println("Hello World!");
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement