2. The ‘static’ keyword in java is mainly
used for memory management.
‘static’ is applied to variables, methods,
blocks and nested classes.
3. The variables and methods declared
in the class are referred as instance
members because a new copy of each
of them is created for each object.
A member that is common to all the
objects and accessed without using a
particular object, those are declared
using static keyword.
4. Example:
static int count;
static int max(int x, int y);
The members that are declared as
static are referred as static members,
and the static variables, static methods
are also referred as class variables,
and class methods.
5. ‘static’ variables
If you declare any variable with ‘static’, it is
known as ‘static variable’.
The ‘static’ variable is used to refer common
properties of all objects(not unique for each
object).
Ex: Company name of all employees,
College name for all the students.
It gets memory only once in class area at the
time of class loading.
6. ‘static’ methods
‘static’ keyword is used with any
method, is called ‘static method’.
‘static method’ belongs to the class
rather than object of a class.
7. Methods declared as static have several
restrictions.
1. They can only call other static methods.
2. They must only access static data.
3. They cannot refer to this or super in
any way.
4. We can access the static member by
using the class name itself.
8. Example 1:
//Defining and using static members
class MathOperation
{
static float mul(float x,float y)
{
return x*y;
}
static float divide(float x,float y)
{
return x/y;
}
}
9. class MathApplication
{
public static void main(String args[])
{
float a=MathOperation.mul(4.0,5.0);
float b=MathOperation.divide(a,2.0);
System.out.println("b="+b);
}
}
12. Static Block
A static block is a block of statements declared as
static, something like this:
static
{
Statements;
}
JVM executes a static block on a highest priority
bases. This means JVM first goes to static block
even before it looks for the main() method in the
program.
14. Static class
When we declare a certain member as
static, we do not have to create an
instance of the class to access it.
Syntax
class Outer
{
static class Nested
{
}
}
Here we have declared an Outer class
and then declared a nested class.
15. Static classes in Java can be created
only as nested classes.
Inner class- Inner classes are the
classes that are non-static and nested.
They are written inside an outer class.
We are unable to create an instance of
the inner class without creating an
instance of its given outer class.
This is needed when the user has to
create a class but doesn't want other
classes to access it. Here we can use
an inner class for that reason.
16. Outer class- Outer classes are the
classes in which nested or inner classes
are defined.
Nested class- Nested class can be static
or non-static. The non-static nested
class is known as an inner class.
An instance of a static nested class
can be created without the instance of
the outer class. The static member of the
outer class can be accessed only by the
17. ‘static’ class
Static class in Java is a nested class and it
doesn't need the reference of the outer
class.
Static class can access only the static
members of its outer class, cannot access
the non-static members.
Inner classes can access the static and
the non-static members of the outer class.
18.
19. class Outer {
// static member of the outer class
private static char grade = 'A';
// Static class
static class Nested {
//non-static method
public void fun() {
// nested class can access the static
members // of the outer class
System.out.println("Grade: " + grade);
}
}
20. public static void main(String args[]) {
Outer.Nested obj = new Outer.Nested();
//creating an object of nested
// class without creating an object
// of the outer class.
obj.fun();
}
}
21. Example 2:
class TestOuter1{
static int data=30;
static class Inner{
void msg()
{
System.out.println("data is "+data);}
}
public static void main(String args[])
{
TestOuter1.Inner obj=new TestOuter1.Inner
();
obj.msg();
}
}