Java | Static Keyword In java :

             

             ~~ CORE-JAVA ~~

 1. INTORDUCTION

     1.1 Java Intorduction
     1.2 Java history
     1.3 Java features
     1.4 Diff b/w Java & C++
     1.5 Java Variables
     1.6 Java Data Types
     1.7 Typecasting
     1.8 Arrays

 2. OOPS

     2.1 Introduction
     2.2 Class
     2.3 Object
     2.4 Static Keywords
     2.5 Constructors
     2.6 This Key Word

 3. INHERITANCE

     3.1 Inheritance (IS-A) 
     3.2 Aggregation (HAS-A)

 4. POLYMORPHISM

     4.1 Polymorphism & Its Type
     4.2 Method Overloading
     4.3 Method Overriding
     4.4 Super Keyword
     4.5 Final Keyword
     4.6 Dynamic Binding

 5. ABSTRACTION

     5.1 Abstract Classes & Methods
     5.2 Interfaces
     5.3 Abstract vs Interface

 6. ENCAPSULATION

     6.1 Encapsulation

 7. STRING MANIPULATIONS

     7.1 String
     7.2 String Buffer
     7.3 String Tokenizer

 8. PACKAGES

     8.1 Predefined packages
     8.2 Userdefined Packages
     8.3 Access Specifiers

 9. EXCEPTION HANDLING

     9.1 Introduction
     9.2 Pre Defined Exceptions
     9.3 Try-Catch-Finally
     9.4 Throws, throw
     9.5 User Defined Exception

 10. MULTITHREADING

     10.1 Introduction
     10.2 Thread Creations
     10.3 Thread Life Cycle
     10.4 Life Cycle Methods
     10.5 Synchronization
     10.6 Wait() notify() notify all()

 11. WRAPPERCLASSES

     11.1 Introduction
     11.2 Wrapper Classes

 12. COLLECTION FRAME WORK

     12.1 Introduction
     12.2 List interface
     12.3 Set interface                                 12.4 Map interface                           

 13. INNER CLASSES

     13.1 Introduction
     13.2 Member inner class
     13.3 Static inner class
     13.4 Local inner class
     13.5 Anonymous inner class

 14. CoreJava Interview Questions

 

In Java we have “Static Keyword”, which helps us to manage memory in better way. We can use Static Keyword with :> Method, Variable, Block and Nested Class.

static keyword in java

1. Static Variable :

  • In java if we are declaring any variable with “static keyword” then, it means it’s “static variable” as below example.
  • If a value of a variable is changing Object to Object then we should go for Instance variable, but if value of a variable is same for all Object then we should go for “Static Variable”.
  • In Instance variable for every Object, separate copy will be created, but in Static variable Single Object will be created and all other Object will share same.
  • Static variables will be crated at the time of class loading and it will destroy at the time of class unloading, so we can say “scope of the static variable is exactly same as the scope of  .class file”.
  • We can declare Static variable anywhere in the Class, but it should be outside of any method or block or any constructor.
  • We can use Static Variable from in any method but we can’t declare under any method.
  • We can access Static variable :
    •  Directly – If static variable is there in same Class, then we can access Directly.
    • With the help of Class Name – If static variable is in different Class, then we have to access with Class Name.
  • Static Variable also know as Class level Variable

Note : We can access Static Var with the help of Object also, but it’s not recommended.

Static Keyword in Java for Static Variable | Example :

 

2. Static Method :

  • In java we have to use static keyword, to declare any static method.
  • We can access static method by :
    •  Directly – If static method is there in same Class, then we can access Directly.
    • With the help of Class Name – If static method is in different Class, then we have to access with Class Name.
  • A static method can access any static variables/members, and it can change the value also.
  • A static method can’t access non static variables or non-static methods directly.

Static Keyword in Java for Static Method | Example :

 

3. Static Block :

  • In Java if we are declaring any block with static Keyword then, it means it’s a static block only.
  • Static Block will execute at the time of class loading only, So suppose we want to perform some activity at the time of class loading then we can put all those things under Static block.
  • In a Class we can create any number of Static Block, but we have to remember it will execute in same sequence whatever we used created.
  • For JDK 1.7 or higher version, we can happily execute any java class without main() method, as below.

 

Static Keyword in Java for Static Block | Example :

 

4. Nested Class :

 

Why the main () method in Java is always static?

 

Is it possible to Run a Java Program without Main method ?