Members of Class
Following members can be defined in a class:
- Variables
- Blocks
- Constructors
- Methods
- Inner Classes
3.4.1 Variables
β A variable is a container which holds user data.
β Memory will be allocated for the variable while executing the program.
β Value of the variable can be changed any number of times during program execution.
Types of Variables (based on data type)β
- Primitive Variables
- Reference Variables
Types of Variables (inside a class)β
- Instance Variables
- Static Variables
β Both Instance Variables and Static Variables can be:
- Primitive
- Reference
Exampleβ
class Hello {
int a; // Primitive Instance Variable
String str; // Reference Instance Variable
static int b; // Primitive Static Variable
static String msg; // Reference Static Variable
}
3.4.1.1 Instance Variables
β Variables defined in a class without using static modifier are called instance variables.
β Instance variables are also called non-static variables.
β Memory will be allocated for instance variables at the time of creating an object.
π‘ Quick Understandingβ
- Variable = data holder π¦
- Instance variable β belongs to object
- Static variable β belongs to class
- Memory:
- Instance β allocated during object creation
- Static β allocated during class loading
Lab287.javaβ
class Lab287 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.a);
h.a = 99;
System.out.println(h.a);
}
}
class Hello {
int a;
}
Lab288.javaβ
class Lab288 {
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;
}
Lab289.javaβ
class Lab289 {
public static void main(String[] args) {
new Hello().a = 99;
System.out.println(new Hello().a);
}
}
class Hello {
int a;
}
Lab290.javaβ
class Lab290 {
public static void main(String[] args) {
Hello h = null;
System.out.println(h.a);
}
}
class Hello {
int a;
}
Lab291.javaβ
class Lab291 {
public static void main(String[] args) {
System.out.println(Hello.a);
}
}
class Hello {
int a;
}
Lab292.javaβ
class Lab292 {
int a;
public static void main(String[] args) {
System.out.println(a);
}
}
3.4.1.2 Static Variables
β Variables defined in the class using static modifier are called static variables.
β Static variables are also called class variables.
β Memory will be allocated for static variables at the time of loading the class.
π‘ Quick Understandingβ
- Instance variable:
- Access β object (
h.a) β - Memory β object creation
- Access β object (
- Static variable:
- Access β class name (
ClassName.var) β - Memory β class loading
- Access β class name (
β οΈ Important Observations from Labsβ
- Each object has its own copy of instance variables
new Hello().a = 99β creates one object, but nextnew Hello()is a different object- Accessing variable using
nullreference β Runtime error (NullPointerException) - Instance variables cannot be accessed using class name
- Static method cannot access non-static variable directly
Lab293.javaβ
class Lab293 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println(h.b);
h.b = 99;
System.out.println(h.b);
}
}
class Hello {
static int b;
}
Lab294.javaβ
class Lab294 {
public static void main(String[] args) {
Hello h1 = new Hello();
Hello h2 = new Hello();
System.out.println(h1.b + "\t" + h2.b);
h1.b = 99;
System.out.println(h1.b + "\t" + h2.b);
}
}
class Hello {
static int b;
}
Lab295.javaβ
class Lab295 {
public static void main(String[] args) {
System.out.println(Hello.b);
}
}
class Hello {
static int b;
}
Lab296.javaβ
class Lab296 {
public static void main(String[] args) {
Hello h = null;
System.out.println(h.b);
}
}
class Hello {
static int b;
}
Lab297.javaβ
class Lab297 {
public static void main(String[] args) {
new Hello().b = 99;
System.out.println(new Hello().b);
}
}
class Hello {
static int b;
}
Lab298.javaβ
class Lab298 {
static int b;
public static void main(String[] args) {
System.out.println(b);
}
}
3.4.2 Blocks
β Block is a set of instructions defined in curly braces { }.
β Block doesnβt have any name, so you cannot invoke it explicitly.
β Block will be invoked by the JVM automatically.
β Blocks defined inside the class directly as a member are called INITIALIZATION BLOCKS.
Types of Initialization Blocksβ
- Instance Initialization Block (IIB)
- Static Initialization Block (SIB)
π‘ Key Understanding (Important)β
- Static variable (
static int b) is:- Shared across all objects
- Only one copy exists per class
- Access ways:
Hello.b// recommendedh.b// allowed but not recommended
- Even if reference is
null:Hello h=null;System.out.println(h.b);// Works (no NullPointerException) - Why? β Because static variables belong to class, not object
3.4.2.1 Instance Initialization Blocks (IIB)
β Block defined inside the class directly without static modifier is called as INSTANCE INITIALIZATION BLOCK.
β IIB will be invoked by the JVM automatically after initializing the object.
3.4.2.2 Static Initialization Blocks (SIB)
β Block defined inside the class directly with static modifier is called as STATIC INITIALIZATION BLOCK.
β SIB will be invoked by the JVM automatically after initializing the class.
Exampleβ
class Hello {
int a;
static int b;
{
System.out.println("I am IIB");
}
static {
System.out.println("I am SIB");
}
}
Lab299.javaβ
class Lab299 {
public static void main(String[] args) {
int a;
a = 10;
System.out.println(a);
}
}
Lab300.javaβ
class Lab300{
public static void main(String[] args){
Hello h=new Hello();
System.out.println(h.a);
}
}
class Hello{
int a;
a=10;// Not allowed (compile-time error)
}
Lab301.javaβ
class Lab301{
public static void main(String[] args){
System.out.println(Hello.a);
}
}
class Hello{
static int a;
a=10;// Not allowed (compile-time error)
}
Lab302.javaβ
class Lab302 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println("Main: " + h.a);
}
}
class Hello {
int a;
{
a = 10;
System.out.println("Initialized: " + a);
}
}
π‘ Key Points (Important)β
- You cannot assign values directly inside class body (outside block/constructor/method)
int a;a=10;// invalid
- Valid ways to initialize:
- Instance Initialization Block (IIB)
- Constructor
- At declaration
int a=10;// valid - π Execution order:
- Static Block (SIB) β runs once when class loads
- Instance Block (IIB) β runs every time object is created
- Constructor β runs after IIB
Lab303.javaβ
class Lab303 {
public static void main(String[] args) {
System.out.println("Main : " + Hello.a);
}
}
class Hello {
static int a;
{
a = 10;
System.out.println("Initialized : " + a);
}
}
Lab304.javaβ
class Lab304 {
public static void main(String[] args) {
System.out.println("Main : " + Hello.a);
}
}
class Hello {
static int a;
static {
a = 10;
System.out.println("Initialized : " + a);
}
}
Lab305.javaβ
class Lab305 {
public static void main(String[] args) {
System.out.println(Hello.a);
System.out.println(Hello.a);
}
}
class Hello {
static int a = 10;
{
System.out.println("Instance Block");
}
static {
System.out.println("Static Block");
}
}
Lab306.javaβ
class Lab306 {
public static void main(String[] args) {
Hello h = null;
System.out.println("Ref Created");
}
}
class Hello {
static int a = 10;
{
System.out.println("Instance Block");
}
static {
System.out.println("Static Block");
}
}
Lab307.javaβ
class Lab307 {
public static void main(String[] args) {
Hello h = null;
System.out.println("Ref Created");
h = new Hello();
}
}
class Hello {
{
System.out.println("Instance Block");
}
static {
System.out.println("Static Block");
}
}
Lab308.javaβ
class Lab308 {
public static void main(String[] args) {
Hello h = new Hello();
new Hello();
}
}
class Hello {
{
System.out.println("Instance Block");
}
static {
System.out.println("Static Block");
}
}
π‘ Key Concepts from this Pageβ
πΉ Static Block (SIB)β
- Runs only once
- Executes when class is loaded
- Triggered when:
- Static variable accessed
- Static method used
- Object created
πΉ Instance Block (IIB)β
- Runs every time object is created
- Executes before constructor
πΉ Important Observationsβ
- Accessing
Hello.aβ triggers static block only - Creating object β triggers:
Static Block (only once)Instance Block (every object)
πΉ Execution Examplesβ
Lab305 Outputβ
Static Block
10
10
Lab306 Outputβ
Ref Created
Lab307 Outputβ
Ref Created
Static Block
Instance Block
Lab308 Outputβ
Static Block
Instance Block
Instance Block
Lab309.javaβ
class Lab309 {
public static void main(String[] args) {
System.out.println("Main : " + Hello.a);
}
}
class Hello {
static int a;
int b;
static {
a = 10;
b = 20; // Not allowed (non-static variable in static block)
System.out.println("Static Block");
}
}
Lab310.javaβ
class Lab310 {
public static void main(String[] args) {
System.out.println("Main in Lab310");
}
static {
System.out.println("Static Block in Lab310");
}
}
3.4.2.3 Local Variablesβ
β Variables can be declared at two places:
- Inside the class directly as a member
- Inside the members of the class
β Variables declared inside class members like methods, constructors, and blocks are called LOCAL VARIABLES.
Lab311.javaβ
class Lab311 {
public static void main(String[] args) {
Hello h = new Hello();
}
}
class Hello {
int a = 10;
static int b = 20;
{
int c = 30; // local variable
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Outputβ
10
20
30
Lab312.javaβ
class Lab312 {
public static void main(String[] args) {
Hello h = new Hello();
}
}
class Hello {
int a;
static int b;
{
int c = 30;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Outputβ
0
0
30
π‘ Key Conceptsβ
πΉ Static Block Ruleβ
- Static block cannot access non-static variables directly
static{
b=20;// invalid if b is non-static
}
πΉ Local Variablesβ
- Declared inside:
- Methods
- Constructors
- Blocks (like IIB/SIB)
- β Must be initialized before use
- β No default values (unlike instance/static variables)
πΉ Default Valuesβ
| Variable Type | Default Value |
|---|---|
| int (instance/static) | 0 |
| local variable | no default (must assign) |
πΉ Execution Order Reminderβ
- Static block
- Main method starts
- Object creation β Instance block
Lab313.javaβ
class Lab313 {
public static void main(String[] args) {
Hello h = new Hello();
}
}
class Hello {
int a;
static int b;
{
int c; // local variable not initialized
System.out.println(a);
System.out.println(b);
System.out.println(c); // compile-time error
}
}
Lab314.javaβ
class Lab314{
public static void main(String[] args){
System.out.println("Main : "+Hello.a);
}
}
class Hello{
static int a;
static{
static int b;// not allowed
System.out.println(a);
System.out.println(b);
}
}
Lab315.javaβ
class Lab315 {
public static void main(String[] args) {
System.out.println("Main : " + Hello.a);
}
}
class Hello {
static int a;
static {
final int b = 20; // allowed
System.out.println(a);
System.out.println(b);
}
}
Lab316.javaβ
class Lab316 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println("Main : " + h.a);
}
}
class Hello {
int a = 10;
{
System.out.println("I. Block 1 : " + a);
}
{
System.out.println("I. Block 2 : " + a);
}
}
Lab317.javaβ
class Lab317 {
public static void main(String[] args) {
System.out.println("Main : " + Hello.a);
}
}
class Hello {
static int a = 10;
static {
System.out.println("S. Block 1 : " + a);
}
static {
System.out.println("S. Block 2 : " + a);
}
}
Lab318.javaβ
class Lab318 {
public static void main(String[] args) {
Hello h = new Hello();
}
}
class Hello {
{
int a = 10; // local variable
System.out.println("I. Block 1 : " + a);
}
{
String a = "JavaWorld"; // local variable
System.out.println("I. Block 2 : " + a);
}
}
π‘ Key Concepts from this Pageβ
πΉ Local Variablesβ
- Must be initialized before use
- No default values β causes compile-time error
πΉ Static Block Rulesβ
- Cannot declare
staticvariables inside static block - Only local variables allowed inside blocks
πΉ Final Keyword in Blocksβ
finallocal variables are allowed- Must be initialized at declaration
πΉ Multiple Blocks Execution Orderβ
Instance Blocks (Lab316)β
I. Block 1
I. Block 2
Main : 10
Static Blocks (Lab317)β
S. Block 1
S. Block 2
Main : 10
πΉ Block Scopeβ
- Variables declared in one block are not accessible in another block
- Each block has its own local scope
Lab319.javaβ
class Lab319 {
public static void main(String[] args) {
Hello h = new Hello();
}
}
class Hello {
{
int a = 10;
System.out.println("I. Block 1 : " + a);
}
{
System.out.println("I. Block 2 : " + a); // a not accessible here
}
}
Lab320.javaβ
class Lab320 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println("Main : " + h.a); // cannot access local variable
}
}
class Hello {
{
int a = 10; // local variable
System.out.println("I. Block : " + a);
}
}
Lab321.javaβ
class Lab321 {
public static void main(String[] args) {
System.out.println("Main : " + Hello.a); // invalid
}
}
class Hello {
static {
int a = 10; // local variable
System.out.println("S. Block : " + a);
}
}
π‘ Key Concepts from this Pageβ
πΉ Scope of Local Variablesβ
- Local variables are accessible only within the block they are declared
- Cannot access:
- In another block
- Outside the block (like in
mainor other methods)
πΉ Examples Explainedβ
Lab319β
ais declared in Block 1- Block 2 cannot access it β compile-time error
Lab320β
ais a local variable inside block- Cannot access using
h.aβ compile-time error
Lab321β
ais inside static block- Cannot access as
Hello.aβ compile-time error
Important Ruleβ
β Local variables:
- Have block-level scope
- Must be initialized before use
- Cannot be accessed outside their block
π§ Extra Noteβ
β Only final modifier can be applied to local variables
Not allowed for local variables:
public,private,protectedstatic,volatile, etc.
3.4.2.4 Local Blocks
β Block can be defined at two places:β
- Inside the class directly as a member
- Inside the members of the class
β Definitionβ
- Blocks defined inside class members like methods, blocks, constructors are called LOCAL BLOCKS
β Executionβ
- Local blocks will be executed only when the enclosing class member is executed
Lab322.javaβ
class Lab322 {
public static void main(String[] args) {
System.out.println("\n Main : " + Hello.a);
}
}
class Hello {
static int a;
{
System.out.println("\n I am IIB");
{
System.out.println("I am Local Block in IIB");
}
}
static {
System.out.println("\n I am SIB");
{
System.out.println("I am Local Block in SIB");
}
}
}
Lab323.javaβ
class Lab323 {
public static void main(String[] args) {
new Hello();
System.out.println("\n**Hello Guys");
}
}
class Hello {
static int a;
{
System.out.println("\n I am IIB");
{
System.out.println("I am Local Block in IIB");
}
}
static {
System.out.println("\n I am SIB");
{
System.out.println("I am Local Block in SIB");
}
}
}
Lab324.javaβ
class Lab324 {
public static void main(String[] args) {
System.out.println("I am main()");
{
System.out.println("I am Local Block in main()");
}
}
}
Lab325.javaβ
class Lab325 {
public static void main(String[] args) {
Hello h = new Hello();
System.out.println("Main : " + h.a);
}
}
class Hello {
{
int a = 10;
String a = "JavaWorld"; // duplicate variable
System.out.println("I. Block : " + a);
}
}
π‘ Key Conceptsβ
πΉ Local Blocksβ
- A block inside another block/method/constructor
- Written using
{ }
πΉ Execution Behaviorβ
| Block Type | When it runs |
|---|---|
| Local block in method | When method executes |
| Local block in IIB | When object is created |
| Local block in SIB | When class is loaded |
πΉ Important Rulesβ
- Local blocks depend on the enclosing block
- They cannot execute independently
- Variables declared inside are local scope only
Important Errors (Lab325)β
- Cannot declare same variable name twice in same scope
- Type conflict (
int aandString a)
π§ Quick Summaryβ
- Local block = block inside block
- Scope = limited
- Execution = depends on outer structure
Lab326.javaβ
class Lab326 {
public static void main(String[] args) {
System.out.println("I am main()");
{
int a = 10;
System.out.println("I am Local Block 1 in main() : " + a);
}
{
int a = 20;
System.out.println("I am Local Block 2 in main() : " + a);
}
{
String a = "JavaWorld";
System.out.println("I am Local Block 3 in main() : " + a);
}
}
}
β Output:β
I am main()
I am Local Block 1 in main() : 10
I am Local Block 2 in main() : 20
I am Local Block 3 in main() : JavaWorld
Lab327.javaβ
class Lab327 {
public static void main(String[] args) {
System.out.println("I am main()");
int a = 90;
int a = 10; // duplicate variable
System.out.println("I am Local Block 1 in main() : " + a);
}
}
Result:β
- Compilation Error
- Reason: Variable 'a' is already defined in the same scope
Lab328.javaβ
class Lab328 {
public static void main(String[] args) {
System.out.println("I am main()");
{
int a = 10;
System.out.println("I am Local Block 1 in main() : " + a);
}
int a = 90;
System.out.println("I am main() : " + a);
}
}
β Output:β
I am main()
I am Local Block 1 in main() : 10
I am main() : 90
π§ Key Concepts (Very Important)
πΉ 1. Scope Ruleβ
- A variable is accessible only within its block
- Same variable name can be reused in different blocks
β Valid:
{
int a=10;
}
{
int a=20;// allowed (different block)
}
Invalid:
int a=10;
int a=20;// same scope β error
πΉ 2. Local Variable Rulesβ
- Must be declared and initialized before use
- No default values provided by JVM
- Scope is limited to
{ }
πΉ 3. Duplicate Variable Ruleβ
- Same variable name cannot be declared twice in same scope
- β Allowed in separate blocks
πΉ 4. Shadowing / Re-declaration Behaviorβ
- Inner block can reuse variable name
- Outer variable remains unaffected
Important Summary (from handwritten notes)β
- In one scope β same variable cannot be defined twice
- Example:
int a=10;
int a=20;// error
- Two classes with the same name cannot be defined in same program
π Quick Understandingβ
| Case | Allowed? | Reason |
|---|---|---|
| Same variable in same block | Duplicate | |
| Same variable in different blocks | β | Different scope |
| Variable after block ends | β | Previous scope ended |