-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUseOfStaticDemo.java
More file actions
46 lines (40 loc) · 1.22 KB
/
UseOfStaticDemo.java
File metadata and controls
46 lines (40 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Student
{
int rollno; // Instance Variable
// Instance Variable Memory Created When Object is Created..
String name;
// I Want to Count How Many Student OBject is Created..
static int counter;
// To intialize the static member java has static block
static{
//counter = 1000; // Eager (During Class Load)
//rollno ++; // Lazy (During Object Creation)
System.out.println("Static will Call When Class is loaded");
}
// init block
{
System.out.println("This is Init Block and It is Pre Constructor Call");
// U Can initalize Ur Instance Variables Here
}
// static member memory is created when class is loaded.
// Class is loaded once
// Constructor is called when object is created
// Constructor role is to initalize the instance variables
Student(int rollno, String name){
this.rollno = rollno;
this.name = name;
this.counter++;
System.out.println("Student Object is Created.."+
this.rollno + " Name "+this.name+" Counter is "+this.counter);
}
{
System.out.println("This is init2");
}
}
public class UseOfStaticDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student ram = new Student(1001,"Ram");
Student shyam = new Student(1002,"Shyam");
}
}