Home Help Java S static keyword in java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

static keyword in java

Advertisement
We are going to discuss about static keyword in java. The static keyword is a special keyword in java programming language. A static member belongs to a class and is not linked with an instance of a class. We can use static keyword, static method, static variable.

static keyword in java

We are going to discuss about static keyword in java. The static keyword is a special keyword in java programming language. A static member belongs to a class and is not linked with an instance of a class. We can use static keyword, static method, static variable.

A static keyword points the following:

  • static methods.
  • static variable.

 static methods:-

static method are unique function in java program. In static method we call other static methods only and we do not call a non static method. In static method we can directly access the class name and do not need any object. Also in static method we can access only static information we can not access non static information.

Syntax of static method:-

<class-name>.<method-name>

static variable:-

A static variable is a special function in java programming language. We can create single copy of all instance of a class. In a static variable we can directly access the class name and do not need any object. We can only initialize it once at the start of the execution.

Syntax of static variable:-

static type varName = value;

Example of static method and static variable

class StaticTest {
int x; //initialized to zero.
static int y; 
 
  StaticTest(){
   //static variable y
   y++;
  }
 
   public void showData(){
      System.out.println("Value of x = "+x);
      System.out.println("Value of y = "+y);
   }

}
 
 public class Test{
   public static void main(String args[]){
     StaticTest s1 = new StaticTest();
     s1.showData();
     StaticTest s2 = new StaticTest();
     s2.showData();
     //Student.b++;
     //s1.showData();
  }
}

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 3, 2013

Related Tutorials

Ask Questions?    Discuss: static keyword in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
D Mahesh kumar yadav
June 3, 2013
java

i am fresher looking for job in java
DMCA.com