Static Members-Java.pptx

718 views 22 slides Jun 04, 2023
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Java


Slide Content

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 { i nt rollno ; String name; static String college=“VLITS”; static void change() { c ollege=“ Vignan University”; } Student( int r,int n) { r ollno =r; name=n; } v oid display() { System.out.println (roll no+” ”+name+” ”+college); } }

class StudentsInfomethod { p ublic 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”); s 1.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 m ain 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 nested class.

‘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