Home | Java - Understanding static

Chapter: Web or internet Programming : JAVA

Java - Understanding static

There will be times when you will want to define a class member that will be used independently of any object of that class.

Understanding static

 

There will be times when you will want to define a class member that will be used independently of any object of that class. Normally, a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static.

 

 

Instance variables declared as static are, essentially, global variables.

 

Methods declared as static have several restrictions:

 

• They can only call other static methods.

 

• They must only access static data.

 

• They cannot refer to this or super in any way.

 

 

//  when the member is static it can be accessed //before any object can be created

 

public class supercla {

 

static int a=3; static int b;

 

/*static method access only static variable

 

*  call ststic method.

 

*  cant be used by this & super keyword*/ static void meth(int x){

 

System.out.println("X=" +x); System.out.println("a="+a); System.out.println("b="+b);

}

 

//Static block loaded exactly once when the //class is first loaded

 

static{

 

System.out.println("Static block"); b=a*10;

 

}

 

public static void main(String args[]){ meth(50);

 

}

 

}

 

 

Inheritance Basics

 

To inherit a class, you simply incorporate the definition of one class into another by using the extends keyword.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : JAVA : Java - Understanding static |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.