Solved

Static keyword in java

Posted on 2004-09-02
8
7,191 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

763 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

12 Experts available now in Live!

Get 1:1 Help Now