Core Java(Part-8)

Content

.Variable

.static

.abstract

Variable

In Java we can create variable in three different way

.instance variable

.local variable

.static variable

Instance variable

the variable which is declare inside class but not inside the method is known as instance variable

instance variable can be used anywhere within the class the scope is within the method

class Demo{
    int instanceVar=10;//instance variable

}

the area (scope) of variable instanceVar is in between curly braces {} of Demo class you can access this variable in other method also. but the method should be in Demo class

class Demo{
    int instanceVar=10;
    public void var(){
        System.out.println(a);
    }
}

In Instance variable all primitive datatype have default value

Data TypeDefault Value
byte, short, int, long0
float, double0.0
char\u0000 (empty character)
booleanfalse
Object (String, arrays, etc.)null
class Example {
    int a;         // default 0
    boolean b;     // default false
    String c;      // default null

    void show() {
        System.out.println(a); // 0
        System.out.println(b); // false
        System.out.println(c); // null
    }

    public static void main(String[] args) {
        Example ex = new Example();
        ex.show();
    }
}

Local variable

the variable which is declare inside the methods

the scope of variable localVar is within the method where that variable declare.

public LocalVariable {
    public void localvariable () {
        int localVar = 12;//local variable
    }
}

the scope of the localVar is within the localvariable(){} method curly braces.

Static variable

Static variable = One copy for everyone.

.It's like a common fridge in a hostel — all students share the same fridge!

.No matter how many objects you create, static variable stays the same for all.

.The variable which is made using static keyword.

.The static variable gets a place during class loading.

.You use static variable in non static place but you can’t use non static in static place

class Example {
    static int count = 0;  // static variable

    Example() {
        count++;  // increasing static variable
        System.out.println(count);
    }

    public static void main(String[] args) {
        Example a = new Example(); // prints 1
        Example b = new Example(); // prints 2
        Example c = new Example(); // prints 3
    }
}

Instance Variable

Each object has its own personal copy.

Static Variable

All objects share one single copy.

You can access static variable directly with the help of class name eg:- (className.variableName) Example.count from different class also.

You can access static variable by reference variable also but it is not a correct way the JVM gets confuse that it is instance variable or static variable, so always use class_Name.static_variable_name when you want to access static variable

  • Static variable belongs to class, not objects.

  • Changes made by one object affect all objects.

  • You can access it by ClassName.staticVariable too!

Static method

You can directly access static method by using class_name.method_name()

class Static2{
    static int a=12;
    public static void print(){
        System.out.println(a);
    }

}

public class StaticMethod {
    public static void main(String args[]){
        Static2.print();
    }
}

abstract class

We create abstract class by using abstract keyword.

In abstract method there is no body like this

We can’t create object of abstract class

A abstract class may or may not have abstract methods

It is used to achieve abstraction but it doesn’t provide 100% abstraction because it can have concrete methods

Note:- It can have abstract and non abstract methods

Methods that are declared without any body within an abstract class are called abstract method

The method body will be defined by it’s sub class

Abstract method can never be final and static

Any class that extent an abstract class must be implemented abstract methods

public abstract void engine();

In abstract class we can write combination of multiple abstract method or normal method.

In child class it is mandatory to override the abstract method in class or if you didn.t want to override that then you have declare that method abstract in child class also.


abstract class Vehicle{
    public abstract void engine();
}
class BMW extends Vehicle{
    public void engine(){
        System.out.println("4000cc Engine");
    }
}

public class AbstractDemo {
    public static void main(String args[]) {
        Vehicle v = new BMW();
        v.engine();
    }
}

We didn’t create the object of abstract class but the abstract class have constructor why?

Abstract class has a constructor because child classes need to initialize the parent part first.

Abstract class means:
➔ You cannot create an object directly.
➔ But it can have a constructor.

Because when a child class (which extends the abstract class) creates an object,
the abstract class constructor gets called first (automatically).

abstract class Animal {
    Animal() {
        System.out.println("Animal constructor called");
    }
}

class Dog extends Animal {
    Dog() {
        System.out.println("Dog constructor called");
    }
}

public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
    }
}
Animal constructor called
Dog constructor called
ConceptSimple Explanation
Abstract class can't make objectBut constructor still needed
Why constructor?Because child class needs to call parent's constructor first.
0
Subscribe to my newsletter

Read articles from Khelendra Kumar Thakur directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Khelendra Kumar Thakur
Khelendra Kumar Thakur