static vs non static methods static vs non static data members static and non static data members static and non static methods static and non static variables difference between static and non static variables in java
difference between static and non static variables in java with example static and non static variables in java example difference between static method and instance method in java static and instance methods in java
difference between static and instance methods in java static method vs instance method in java static and instance methods in java synchronization of static and instance methods in java static and instance variables in java with example static and instance variables in java example static vs instance methods in java static vs instance methods in java with example differnece between static and instance methods in java with example differnece between static and instance methods in java example
Static Data members (Methods and variables) Vs Non Static or Instance Data members (Methods and variables):
Instance or Non static Data members(Methods and variables):
Instance or Non – static variable is without
the static keyword means that it is part of an object or it also called
"instance" and not a part of a class. It is referred to by the name
of the specific object to which the method or variable belongs.
1) Instance
variables are those whose memory space is created each and every time whenever
object is created.
2) Instance
variable’s are always used for storing specific values.
3) Programmatically
Instance variable’s declaration should not preceded by static keyword.
Syntax:
Datatype v1,v2…..vn;
4) Each
and every instance variable must be accessed w.r.t object name.
Objectname.Instancedatamembername;
5) Each
and every value of instance variable is not sharable.
Static
Data members(Methods and variables):
Static data members of
class in which the concept of static has to do with whether something
is part of a class or an object (instance).
In the case of the main method
which is declared as static, it says that the main method is an
class method.
A method that is part of a class, not part
of an object. This means that another class could call a class method of
another class, by referring to the Class Name method.
1) Static
data members are those whose memory space is creating only once whenever the
class is loaded into main memory irrespective of number of objects is created.
2) Static
data members are always used for storing common values.
3) Programmatically
static data members declaration must be preceded by static keyword.
Syntax :
Static dataype v1,v2……vn;
4) Each and every static data member must be
accessed w.r.t class name.
Classname.staticdatamembername;
5) Each
and every value of static data member is sharable
The Following Program illustrate the concept of Static and Non static Data members:
//
StaticInstanceDemo.java
Class StaticInstanceDemo
{
int
x=10; // Instance or non – static data
members
static int y=100;// static data member
void f1() // Instance or non – static data members
{
System.out.println(“this is f1()
function”);
StaticInstanceDemo.f2();// calling static
method from non
// - static context.
}
static
void f2()
{
// System.out.println(“value of x”+obj.x);
Cannot
//
non static from static context. error
System.out.println(“this
is f2() function”);
}
public static void main(String args[])
{
{
InstanceDemo obj=new InstanceDemo();
//creation of
//object or reference for Non-static
//member
obj.f1();
StaticInstanceDemo.f2();
}
}
Analysis:
From the above program which
illustrates both static and non static such that x and f1() is instance data
members and they can be accessed by using object or instance, y and f2() is
static data members, they can be accessed by using class name.
Hi, its good to red this blog easy explanations of static and non static variables .
ReplyDeleteThanks for your efforts .
To know more about statics which should be used when you don't want to have varying behavior for different objects.
http://efectivejava.blogspot.in/2013/08/when-to-use-static-members-in-java.html
This comment has been removed by the author.
ReplyDeleteDifference between Static and non-static method in Java
ReplyDeleteThanks for this post