Skip to main content

Members of Class

Following members can be defined in a class:

  1. Variables
  2. Blocks
  3. Constructors
  4. Methods
  5. 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)​

  1. Primitive Variables
  2. Reference Variables

Types of Variables (inside a class)​

  1. Instance Variables
  2. 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
  • Static variable:
    • Access β†’ class name (ClassName.var) βœ”
    • Memory β†’ class loading

⚠️ Important Observations from Labs​

  • Each object has its own copy of instance variables
  • new Hello().a = 99 β†’ creates one object, but next new Hello() is a different object
  • Accessing variable using null reference β†’ 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​

  1. Instance Initialization Block (IIB)
  2. 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// recommended
    h.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:
    1. Static Block (SIB) β†’ runs once when class loads
    2. Instance Block (IIB) β†’ runs every time object is created
    3. 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 TypeDefault Value
int (instance/static)0
local variableno default (must assign)

πŸ”Ή Execution Order Reminder​

  1. Static block
  2. Main method starts
  3. 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 static variables inside static block
  • Only local variables allowed inside blocks

πŸ”Ή Final Keyword in Blocks​

  • final local 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 main or other methods)

πŸ”Ή Examples Explained​

Lab319​

  • a is declared in Block 1
  • Block 2 cannot access it β†’ compile-time error

Lab320​

  • a is a local variable inside block
  • Cannot access using h.a β†’ compile-time error

Lab321​

  • a is 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, protected
  • static, 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 TypeWhen it runs
Local block in methodWhen method executes
Local block in IIBWhen object is created
Local block in SIBWhen 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 a and String 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​

CaseAllowed?Reason
Same variable in same blockDuplicate
Same variable in different blocksβœ”Different scope
Variable after block endsβœ”Previous scope ended