Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is static variable and static method?

Answer Posted / kishore rajkumar

When static is used the VARIABLE or PROPERTY in the class is initialised only once. However, that does'nt mean that the value of the VARIABLE will remain unchanged. What i mean to say is, no matter how many instances of the class you create, your VARIABLE will not be re-initialized to its initial value for each new instance as opposed to a non-static variable.

Let us see an example;


public class StaticTest{

int a; // a non-static variable
static int b; // variable declared as static

StaticTest(){
a++;
b++;
}


public void display(){

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

}


}

class MainTest{

public static void main(String[] args){

StaticTest o1=new StaticTest();
StaticTest o2=new StaticTest();
StaticTest o3=new StaticTest();

o1.display();
}

}

output:
-----------------------------
a=1;
b=3;

EXPLAINATION:
===============

Look in the class " StaticClass ". Here, we have declared two variables 'a' & 'b'. Variable 'a' is a non-static variable whereas 'b' is declared as static.
Also, each time instance of this class(StaticClass) is created 'a' & 'b' are incremented inside the constructor.
Now, in the ' MainTest ' class we have created 3 seperate instances of the class ' StaticClass '.
Here, for each new instance the variable 'a' is re-initialized to its initial value and then incremented by 1.
As a result, its value is always 1 no matter how many instances we create.

But, in case of 'b' the value in not re-initialized everytime a new instance is created. Rather, it is initialized to its initial value only for the first instance and then its value is obtained by all the subsequent instances.
Hence, its value keeps on increasing as more and more instance are created.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many tetrahedral voids are there in bcc?

1086


What is Session reduplication and how its done?

2033


What is the default size of arraylist in java?

1117


What are namespaces in java?

1029


What is an interface in java? Explain

1177


What methodology can be utilized to link to a database?

956


What does three dots mean in java?

1119


What is string and its types?

1176


Can we extend private class in java?

978


What are void pointers?

1293


What are different types of classloaders?

1015


What is ascii code?

1257


which class is the wait() method defined in? : Java thread

939


What is time complexity java?

990


Explain java code for recursive solution's base case?

1024