The static modifier is used to create variables and methods that will exist
independently of any instances created for the class. All static members exist
before you ever make a new instance of a class, and there will be only one copy of
a static member regardless of the number of instances of that class.
all instances of a given class share the same value for any given static variable.
Static can not refer to this or super.
static methods can't be overridden!
Static variables are also known as class variables
When the program starts the static blocks, and variables executed first.
Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks.
We can call the static variables a=by its name directly inside the same class But for other classes we have to call it by its class name.
class Demo{
static{System.out.println("hi this is static block");}
public static void main(String[] args){System.out.println("this is main");}
It will print:
hi this is static block
this is main

Non-static variables/methods can not be called directly by its name inside a static method. We have to call non-static variable/methods in a static method by its instance.
class Demo{
int x=10;
public sttatic void main(String[] args){
Demo d=new Demo();
System.out.println(d.x);//as main() is static and x is non static

In java the program execution begins with the main() method. So it must be called before any object exist.As main() is static it is independent of any object.

Things you can mark as static:

■ Methods
■ Variables
■ A class nested within another class, but not within a method
■ Initialization blocks

Things you can't mark as static:
■ Constructors (makes no sense; a constructor is used only to create instances)
■ Classes (unless they are nested)
■ Interfaces
■ Method local inner classes (we'll explore this in Chapter 8)
■ Inner class methods and instance variables
■ Local variables


class Demo
int x=10;
public static void main(String[] args){
Demo d1=new Demo();
System.out.println(d1.x);//prints 11
Demo d2=new Demo();
System.out.println(d2.x);//prints 11
Demo d3=new Demo();
System.out.println(d3.x);//prints 11

class Demo
static int x=10;
public static void main(String[] args){
Demo d1=new Demo();
System.out.println(d1.x);//prints 11 (static variables can also b called by //object reference(d.x))
Demo d2=new Demo();
System.out.println(d2.x);//prints 12
Demo d3=new Demo();
System.out.println(d3.x);//prints 13

In the first code x is not static so each instance(d1,d2,d3) have their own copy of x(different memory location). For the first instance x(the copy of x for the same instance) is initialized to 10 and the constructor increments the vale by 1. For another instance another x(not the same x of 1st instance) is initialized and constructor increments the value.key point-X is different for all instance

But in the second case x is static. So there is only a single copy of x shared by each instance. Each time instance is created the same x increamented by the constructor.So only the same copy of x is incremented each time.So it gives output 11,12,13.keypoint-x is same for all instances

Hope now you understand static

1 comment: