MY mENU


Thursday 9 February 2012

Static variables and Methods in Java


static variables & methods: Actually the staic keyword is used to say that the member variables or methods are common variables for all the objects of that class.That means " There is only one copy of variables is there for the Class". Hence these variables can be accessed directly using the Class name or normally with the object name.
Eg:
class ab
{
static x;
--------
--------
}

the 'x' value can be accessed outside the class as
class bb
{
------
-------
ab.x;
or
ab a=new ab();
a.x;
------
------
}

static block:


The static block is the class level one time execution block it is mailnly used to do some operations at the time of class loading.
eg:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Here the class JdbcOdbcDriver contains the static block which contains the operations of registering with the DriverManager for establishing the connection.

In java we hav static and non-static(instance). Generally we members which may static or non-static.
Why static:: Suppose we hav to call member (a method or a variable) without or before creation of a an object, then that member should be declared as static member (eg: static int a; / static void mone(). Now these members can be called by using class name dot member(class name.member). And we know that non-static member can be called by using object. This static member will be shared by all the objects from the memory. But non-static members will be available seperate copy for all the objects.

Example for static::

We know every java program will be executed by the JVM program. That means JVM should call the main method before creation of an object for that class. Because of this, main method should be given as static. i,e public static void main(String args[]). Now jvm internally uses class name for calling the main method. Now after calling main method only object will be created..

static method are the methods which can acess only the static variables.
The use of a static method suffers from the following restrictions:

1. A static method can only call other static methods.

2. A static method must only access static data.

3. A static method cannot reference to the current object using keyword this.

Things you can mark as static:
■ Methods
■ Variables
■ A class nested within another class, but not within a method
■ Initialization blocks

Things you can't mark as static:
■ Constructors (makes no sense; a constructor is used only to create instances)
■ Classes (unless they are nested)
■ Interfaces
■ Method local inner classes (we'll explore this in Chapter 8)
■ Inner class methods and instance variables
■ Local variables




Static Block :Whenever you want to excute a code before execution of main mehtod, you can write a code using static block.

Static Method: Whenever you want to call a method using class name instead of using a object of class.

Static Variable : Static Variable will have a single copy per class.


No comments:

Post a Comment