0% found this document useful (0 votes)
132 views5 pages

Static Members

Static members belong to the class rather than individual instances. They can be accessed using the class name without creating an instance. Static variables occupy a single location in memory and static methods can only use static variables, while non-static methods can use both static and instance variables. An example shows using a static variable to count instances and a static method to display the count.

Uploaded by

Pramod kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views5 pages

Static Members

Static members belong to the class rather than individual instances. They can be accessed using the class name without creating an instance. Static variables occupy a single location in memory and static methods can only use static variables, while non-static methods can use both static and instance variables. An example shows using a static variable to count instances and a static method to display the count.

Uploaded by

Pramod kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Static Members

Variables and methods declared using


keyword static are called static members of a
class. We know that non-static variables and
methods belong to instance.

But static members (variables, methods) belong to


class. Static members are not part of any instance
of the class.

Static members can be accessed using class name


directly, in other words, there is no need to create
instance of the class specifically to use them.

Static members can be of two types:

Static Variables
Static Methods
Static Variables
Static variables are also called class
variables because they can be accessed
using class name, whereas, non static
variables are called instance variables and
can be accessed using instance reference
only.

Static variables occupy single location in the


memory. These can also be accessed using
instance reference.

These are accessible in both static and non-


static methods, even non-static methods can
change their values.
While declaration, there is no need to initialize
the static variables explicitly because they
take default values like other non-static
variables (instance variables).

Now, the question is, why to use static


variables?

Let us look at a reason for using one. Suppose


you want to keep record about the number of
running instances of a class. In this situation, it
would be better to declare a static variable.
Static Methods/Class Methods

Static methods are also called class methods. A


static method belongs to class, it can be used
with class name directly. It is also accessible
using instance references.

Static methods can use static variables only,


whereas non-static methods can use both
instance variables and static variables.

Implementation:
The following program uses a static counter
variable and static method showCount() method
to track the number of running instances of the
class student in the memory.
class Student
{ private static int count=0; //static variable
Student()
{
count++; //increment static variable
}
static void showCount() //static method
{
System.out.println(“Number Of students : “+count);
}
}
public class StaticDemo
{
public static void main(String[] args)
{
Student s1=new Student();
Student s2=new Student();
Student.showCount(); //calling static method
Student s3=new Student();
Student s4=new Student();
s4.showCount(); //calling static method
}
}

You might also like