Static Members
 The ‘static’ keyword in java is mainly
used for memory management.
 ‘static’ is applied to variables, methods,
blocks and nested classes.
 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.
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.
‘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.
‘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.
 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.
 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;
}
}
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);
}
}
Example 2:
class Student
{
int rollno;
String name;
static String college=“VLITS”;
static void change() {
college=“Vignan University”;
}
Student(int r,int n) {
rollno=r;
name=n;
}
void display() {
System.out.println(roll no+” ”+name+” ”+college);
}
}
class StudentsInfomethod
{
public static void main(String args[])
{
Student.change()
Student s1=new Student(501,“Pavan”);
Student s2=new Student(502,“Kalyani”);
Student s3=new Student(503,“Vignesh”);
s1.display();
s2.display();
s3.display();
}
}
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.
Example:
class Test
{
static
{
System.out.println("static block");
}
public static void main(String args[])
{
System.out.println("main block");
}
}
Output:
Static block
main block
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.
 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.
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
‘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.
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);
}
}
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();
}
}
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();
}
}
Thank you

Static Members-Java.pptx

  • 1.
  • 2.
     The ‘static’keyword in java is mainly used for memory management.  ‘static’ is applied to variables, methods, blocks and nested classes.
  • 3.
     The variablesand 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 intcount;  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  Ifyou 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 declaredas 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: //Definingand 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 staticvoid main(String args[]) { float a=MathOperation.mul(4.0,5.0); float b=MathOperation.divide(a,2.0); System.out.println("b="+b); } }
  • 10.
    Example 2: class Student { introllno; String name; static String college=“VLITS”; static void change() { college=“Vignan University”; } Student(int r,int n) { rollno=r; name=n; } void display() { System.out.println(roll no+” ”+name+” ”+college); } }
  • 11.
    class StudentsInfomethod { public staticvoid main(String args[]) { Student.change() Student s1=new Student(501,“Pavan”); Student s2=new Student(502,“Kalyani”); Student s3=new Student(503,“Vignesh”); s1.display(); s2.display(); s3.display(); } }
  • 12.
    Static Block  Astatic 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.
  • 13.
    Example: class Test { static { System.out.println("static block"); } publicstatic void main(String args[]) { System.out.println("main block"); } } Output: Static block main block
  • 14.
    Static class  Whenwe 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 classesin 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- Outerclasses 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  Staticclass 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.
  • 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 voidmain(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{ staticint 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(); } }
  • 22.