1. Class:
- Class contains logical description of read word entity in terms of state and behaviour.
- Class = State + Behaviour
Syntax: [modifiers] class <ClassName>{ //members of class }
2. Object:
- Object is the run time entity because memory will be allocated for an object while executing the program as per class description.
- Memory for object will be allocated in HEAP memory.
- It is also called instance of the class.
Syntax: <ClassName><refVarName> = new ClassName(); Ex: Customer c1 = new Customer(); Customer c2 = new Customer(); Customer c3 = new Customer(); class Customer{ int cid; String cname; long phone; void show(){ //... } }
What happens inside the JVM when you created an object:
Customer c1 = new Customer();
1. Allocates 8 bytes of memory for the reference variable and initializes with the null value.
2. Verifies whether the class is loaded or not.
3. Creates an object i.e Allocates the memory for instance variables of the class and initializes with the default values.
4. Address of newly created object will be assigned to the reference variable.
3. Members of a class
Following members are defined inside a class:a. Variables
b. Blocks
c. Constructors
d. Methods
e. Inner Classes
Variables
Memory will be allocated for the variable while executing the program.Types of Variables:
There are two types of variables based on data type used to declare the variable:
- Primitive variable
- Reference variable
Types of Variables:
There are two types of variables which can be declared inside the class directly:
- Instance variable
- Static variable
Instance Variables:
- Variables defined in the class without using static modifier are called instance variables.
- Instance variables are also called as non-static variables.
- Memory will be allocated at the time of creating an object
- Default value is provided by JVM.
- Instance variables cannot be accessed from static context (method/block) directly. Object reference has to be used.
class Test1{ public static void main(String args[]){ Hello h1 = new Hello(); Hello h2 = new Hello(); System.out.println(h1.a+"\t"+h2.a); h1.a = 99; System.out.println(h1.a+"\t"+h2.a); } } class Hello{ int a; }
class Test2{ public static void main(String args[]){ new Hello().a = 99; System.out.println(new Hello().a); } } class Hello{ int a; }
class Test3{ int a; public static void main(String args[]){ System.out.println( a); //Instance variable cannot be accessed directly inside static method } } Output - C.T.E
Static Variables
- Variables defined in the class using static modifier are called as Static variables.
- Also called class variables.
- Memory will be allocated to static variables at the time of loading the class.
- Static variables can be accessed in Static and Non-static context (methods, blocks).
class Test4{ public static void main(String args[]){ Hello h1 = new Hello(); Hello h1 = new Hello(); System.out.println(h1.b +"\t"+h2.b); // 0 0 h1.b=99; System.out.println(h1.b +"\t"+h2.b);// 99 99 //Compiler internally converts it into Hello.b and Hello.b } } class Hello{ static int b; }
Summary:
Instance variables:
- Multiple copies of memory will be allocated for multiple objects.
- Instance variable is related to instance or object of the class so when you change data of one object then that will not affect data of another object.
- Instance variables must be accessed by reference variable which contains object of the class.
- Following are the ways to access instance variable:
Using reference variable that contains object reference Hello h = new Hello(); h.a=90; //VALID Directly using Object reference new Hello().a=90; //VALID (Not recommended because of no reusability)
- When you try to access instance variable with null reference then java.lang.NullPointerException will be thrown at runtime.
- When you try to access instance variable with Class name then error message will be given at compile time called "non-static variable cannot be referenced from a static context".
Hello n = null; h.a = 90;
Hello.a=90;
Static variables:
- Only one copy of memory will be allocated for static variable for multiple objects and single copy of memory will be accessed by all the objects.
- Static variable is related to class so when you change data of static variable then modified data will be accessed by all objects.
- Static variables can be accessed with class name directly and object is not required but you can access static variable with object also.
- Following are the ways to access static variable:
a) With class name Hello.a=90; b) Using the reference variable which contains null Hell h = null; h.b=90; c) Using the reference variable which contains object address Hello h = new Hello(); h.b=90; d) Directly using Object reference: new Hello().b=90;
- When you refer static variable with other than the class name then compiler will change it with class name.
a) Hello h = null; h.b=90; ->Changes to Hello.b=90; b) Hello h = new Hello(); h.b=90; ->Changes to Hello.b=90; c) new Hello().b=90; ->Changes to Hello.b=90;
Initialization Blocks:
- Class can contain only five class members; you can't place any other Java statements directly.
Ex: class Hello{ int a; //VALID a=10; //INVALID } class Hi{ int b = 10; //VALID System.out.println(b); //INVALID }
- IIB will be invoked by the JVM automatically after initializing the Object.
- SIB will be invoked by the JVM automatically after loading the Class.
- Instance variable must be initialized within the Instance block only.
- Static variable should be initialized within the Static block only but it can be initialized inside Instance blocks also.
- Instance block will be executed every time when object is created.
- Static block will be executed only once when the class is loaded.
- Class will be loaded only once when you or JVM uses the class members first time.
- Instance variables cannot be accessed from static context directly but can be accessed with object reference.
Local Variables:
- JVM will not initialize Local variables with default value; it must be initialized by you before using it.
- Scope of Local variable is within the class member where it is declared.
- Local member is not a member of a class so it cannot be accessed using class name or object.
- Local variables cannot be static.
- Only final is allowed for Local variables.
- Memory for Local variables will be allocated in the STACK MEMORY.
No comments:
Post a Comment