Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,491 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,706 people online right now. Registration is fast and FREE... Join Now!




Basics of static

 
Reply to this topicStart new topic

> Basics of static, variables/methods/classes/blocks

bhandari
Group Icon



post 3 Mar, 2008 - 07:47 AM
Post #1


hi,

I will be briefing what a static variable/method/class/block means.

A static literally means stand still. In programming the implication is a variable/method/class/block which is complete in itself i.e. no instantiation is required for using the element.

A variable is complete in itself means that you don't need any object to acces the static field. There are shared by all the objects of the class and hence they can be accessed by the class name as shown below:

java

class test{
static int i = 0;
}

public class tester{
public static void main{
System.out.println(test.i);
}
}


But Sun thought that it would be nice to allow the static variables to be accessed by objects also. So a code example to show this is:
java

class test{
static int i = 0;
}

public class tester{
public static void main{
test t = new test();
System.out.println(t.i);
}
}


A code sample illustrate that the same copy of static variable is used among the objects of the class is as:
java

class test{
static int i = 0;
public test() {
i++;
}
}

public class tester{
public static void main{
test t1 = new test();
System.out.println(t1.i);
test t2 = new test();
System.out.println(t2.i);
}
}


The output of the above code will be
1 2
which proves that same static variable is used for all the objects of the class.

A method is complete in iteslf has the implication of a variable that it can be used without creating any instance of the class. Here is a code sample to illustrate this:

java

class test{
static int testMethod(){
System.out.println("tested static");
}
}

public class tester{
public static void main{
System.out.println(test.testMethod());
}
}


As in case of variables, the static methods can also be accessed by using the objects.
main is the most common example of a method being static.
Please note that a static method can't access non-static members of the class and main method is no exception to it.

A static class is complete in itself means that its memebers can be accessed without creating any instance of the class. Thus in effect all the members of the class become static.
Please note that a top-level class can't be declared as static. The discussion of top-level and inner classes is a big topic in itself and more info about static classes can be found here


Even a code block can be static. But it has to be independent code block inside a class as shown below:
java

class test{
static int i=0;
static{
i++;
}
}

public class tester{
public static void main{
System.out.println(test.i);
}
}


The output will be 1 because the static block is executed while the class is being loaded by the JVM.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/2/08 07:36PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month