简体   繁体   中英

Can a Variable be initialized inside a Class in Java? What about Static Variable? If yes, then what's the use for Static Block?

In C++(prior to C-11), we needed to initialize the variables outside the Class either through constructors or some methods. What's happens in Java?

Can a Variable be initialized inside a Class in Java?

Yes, like this:

public class MyClass {
    private int myVariable = 10;
}

What about Static Variable? If yes, then what's the use for Static Block?

Yes, static variables can be initialized in the class as well:

public class MyClass {
    private static int myVariable = 10;
}

Static blocks are used when you want to initialize a static variable, but one line is insufficient. For example:

public class MyClass {
    private static HashMap<Integer, Integer> myMap;

    static {
        myMap = new HashMap<>();
        myMap.put(10, 20);
        myMap.put(20, 40);
    }
}

c++ v/s Java:

Common - both OOP language difference - c++ is not purely object oriented language, but Java is purely oop language.

Classes are blueprint(like a general map) which defines some attributes/properties(Member variables) and behavior(member functions) of a object of that class.

Class is just a imagination before creation of a object.

Object is real time entity that has physical existence in real world or in simply it's a implementation of class.

Classes in java:

class class_name
{
  member variables;
  member functions;
};

Ex.

class A
{
int a;
void funct()
{
//body
}
};                     //defination is closed with semicolon

but,

classes in java:

ANSWER to ur quesion:

class class_name
{
  member variables;       //still we define the attributes in class that may be static or non-static
  member functions;
};

Significance of static variable: static variable is alloacated the common memory in ram for all the objects of that class and operation perform by any object on static member is reflected to all other object's static member because of common(same) memory.

significance of static method(functions are called methods in java): static method of a class is a method which is called without creating the object of that class. In java, main() method is declared as static because after execution of program main() method is called without creating the object of class.

kernal of OS calls the main() method.

Can a Variable be initialized inside a Class in Java?

Yes.

class TestClass {
    int abc = 0;
    static int def = 1;
}

What about Static Variable?

Yes, you can. Same Example as above.
But this won't be initialized every time an object of the class is created.

TestClass ob1 = new TestClass();
ob1.def = 2; // Always use the class name to access static variables. This is just an example.

TestClass ob2 = new TestClass();
System.out.println(ob2.def); // Output : 2

PS : Always use the class name to access static variables. This is just an example.

What's the use for Static Block?

If the initialization of static variables is complex, then you can create a static block and initialize those variables there. Here is a good reference for the same.

class TestClass {
    int abc = 0;
    static int def = 1;

    static {
         int x = 100;
         int y = 20;
         def = x - y + 10;
    }

}

If you wat to initialize the variable you can create something like

public class Animal
{
    int age = 21;
    static int roll = 23; 
}

But remember the difference between instance variables and static variables, int age - this variable is created for each object you create static int roll - this variable is created only once and is one for every other object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM