Solved

Static keyword in java

Posted on 2004-09-02
8
7,195 Views
Last Modified: 2012-08-13
what is the significance of static keyword in java?.
why do we use it for methods, variables etc?
could u explain me in detail and possibly with some examples?


thanks and regards,
sameer
0
Comment
Question by:SamiirKhan
8 Comments
 
LVL 19

Accepted Solution

by:
ramazanyich earned 25 total points
ID: 11970702
vor variables static keyword means that it is assigned for all instances of class.
For methods statis means that you can call it without creating instance of class.
Example:
in class A you have method:
public statis void method test(){
...
}

So you can call it from another class B:
A.test();

For variables:
Class A has
public static String ss=" test";

In another class you have defined:
Object a = new A();
Object b= new A();

a.ss="another value";
System.out.println(b.ss);

output should be "another value"
0
 
LVL 3

Assisted Solution

by:aravindtj
aravindtj earned 25 total points
ID: 11973790
hi,
Static varialbles/ methods are being only one instance.
Even though you have multiple instances of a object, having static variables/ methods,
there is a only one instance for all those instances.
Static varibales are initialized automatically. Eg. static int a; means a=0, and if its value is modified in any of the instance, it will affected on all the instances.
You can access those static variables/methods with out creating instances of the object.

all the best.
0
 
LVL 16

Assisted Solution

by:suprapto45
suprapto45 earned 25 total points
ID: 11974253
Dear sameer,

Static variable and method belongs to the class and not to the instance of the class. So everytime you create the instance of class, the static variable will remain the same.

Why do you need them? Well, sometimes that you want to have the counter on how many classes instance have been created. This static variable will help you to do it. Static is also useful when you really want to have a single instance of the class to be created. By using static, you can prohibit the user from having more than one instance for one object.

Remember that static method will not be able to be overriden.

I hope it helps

 

 
0
 
LVL 15

Expert Comment

by:babuno5
ID: 11981134
Well as we know that  a class can contain methods and variables that are shared among all instances of the class. These shared members are called static variables and static methods. The most common use of static variables in a class is to hold predefined constants or unchanging objects all of the instances can use.
There are two advantages to this approach. The more obvious advantage is that static members take up space
only in the class; the members are not replicated in each instance. The second advantage is that static
members can be accessed even if no instances of the class exist. A hypothetical Component class might
have a static variable called maximumWidth. Some other class that has to deal with this component, such
as a layout manager, might want to know the maximum width of such a component, even if there aren't any
around at the moment. Since maximumWidth is a static variable, the layout manager can get this
information.
An instance of the Color class represents a color. For convenience, the Color class contains some static,
predefined color objects with friendly names like green, red, and (my favorite) magenta.
Color.green is thus a predefined Color object that is set to a green color. In this case, these static
members of Color are not changeable, so they are effectively constants and can be optimized as such by the
compiler. Constant static members make up for the lack of a #define construct in Java.
0
 
LVL 6

Expert Comment

by:SCDMETA
ID: 11991628
An easy way to think about static variables and methods is:

1) They are like global variables in a class.  All instances of the class share the same "static" variables.
2) The variables and methods exist and can be accessed via the class even though an instances of the class does not exist.



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 52
eclipse package explorer vs project explorer view 2 84
tidtcpserver connection lost handle 2 71
When i run adoquery my application freezes 26 150
This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now