CodeGym /Java Course /Module 2. Java Core /Loading classes, static data…

Loading classes, static data…

Module 2. Java Core
Level 8 , Lesson 1
Available

"Hello, Amigo! I heard Rishi explained something new and exciting to you?!"

"That's right, Kim."

"My topic will be no less interesting. I want to tell you about how classes are loaded into memory."

Classes in Java are files on the disk that contain bytecode, which is compiled Java code.

"Yes, I remember."

The Java machine doesn't load them if it doesn't need to. As soon as there's a call to a class somewhere in the code, the Java machine checks to see if it is loaded. And if not, then it loads and initializes it.

Initializing a class involves assigning values to all of its static variables and calling all static initialization blocks.

"That seems similar to calling a constructor on an object. But what's a static initialization block?"

"If you need to execute complex code (for example, loading something from a file) to initialize objects, we can do it in a constructor. However, static variables don't have this opportunity. But since the need still remains, you can add a static initialization block or blocks to classes. They are basically equivalent to static constructors."

This is how it looks:

Code What really happens
class Cat
{
public static int catCount = 0 ;
public static String namePrefix;

static
{
Properties p = new Properties();
p.loadFromFile("cat.properties");
namePrefix = p.get("name-prefix");
}

public static int maxCatCount = 50;

static
{
Properties p = new Properties();
p.loadFromFile("max.properties");
if (p.get("cat-max") != null)
maxCatCount = p.getInt("cat-max");
}

}


class Cat
{
public static int catCount;
public static String namePrefix;
public static int maxCatCount;

//Static constructors aren't allowed in Java,
//but if they were, everything
//would look like this
public static Cat()
{
catCount = 0;

Properties p = new Properties();
p.loadFromFile("cat.properties");
namePrefix = p.get("name-prefix");

maxCatCount = 50;

Properties p2 = new Properties();
p2.loadFromFile("max.properties");
if (p2.get("cat-max")!=null)
maxCatCount = p2.getInt("cat-max");
}
}

It's a lot like what happens when a constructor is called. I've even written it as a (nonexistent) static constructor.

"Yes, I get it."

"Very good."

Comments (21)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Paul Level 24, Vienna, Austria
4 August 2022
This lesson only added confusion. I don't get the idea of the static block at all! What is is it used for? How do you use it, and where? I found the exercises also really difficult, because I never know where to add the static block
Олег Байбула Level 32, Ukraine Expert
16 January 2023
You can add something to a static list without creation of an object, for example:

static List<String> list = new ArrayList<>();
public class Main {
    static List<String> list = new ArrayList<>();

    static {
        list.add("Hello");
    }
}
MaGaby2280 Level 41, Guatemala City, Guatemala
14 January 2020
If you unlock the next lesson and than you come back, than the code entry will be unlocked...
Andrei Level 41
7 December 2020
Maybe it was a bug, just did that and nothing happened.
Jason Level 16, United States
7 January 2020
as others below have said, the task here is broken. You can't complete this task: you don't have access to it.
yi Level 20, Toronto, Canada
30 December 2019
Code entry: "You can't complete this task: you don't have access to it." ????
Yuliya Sheludyakova Level 19, Dusseldorf, Germany
17 October 2019
I don't understand why do we need to run a static initialization block for the maxCatCount variable when it is already initialized with

maxCatCount = 50
For the namePrefix variable that makes sense.
Seb Level 41, Crefeld, Germany
3 February 2020
Yeah, you're right - it's not really necessary for the maxCatCount variable to be initialized in the static block as it already has a value. But hey, maybe it just shows that it's also possible to overwrite an already existing value via a static block... :-P :-)
Jonaskinny Level 25, Redondo Beach, United States
2 March 2022
For overwriting default values.
Ifeanyi Level 31, Lagos, Nigeria
14 September 2019
Amigo is just getting everything
proegg123 Level 16, Slovakia
21 March 2022
Lucky him :D
Joy Majumdar Level 16, Kolkata, India
7 August 2019
Gone through it 3 times or more , still confusing
Fa Yu Level 16, Tashkent, Uzbekistan
5 July 2019
I didn't understand what is this Properties is? And why it gets some int value from String value (is it some kind of xml file's key String value)?
QUANG PHUONG Level 18, HO CHI MINH, Vietnam
28 May 2019
Java keeps surprising me!
Darko Jakimovski Level 18, Kriva Palanka, Macedonia, The Former Yugoslav Republic of
15 May 2019
Am I the only one who didn't understand crap from this lesson or what?
Ops guy Level 17, Round Rock, TX, United States
16 May 2019
What you don't understand? Static block is executed (and static variables) before you can "use" the class.
Darko Jakimovski Level 18, Kriva Palanka, Macedonia, The Former Yugoslav Republic of
17 May 2019
Yeah it;s all clear now :)
Henk Level 19, Pretoria, South-Africa
22 May 2019
jayyyy! At last someone honest, lol! I don't get what the hell this does. And they're overwriting Properties p = new Properties() in the second Static block. Not sure why. Will probably get more understandable as we go along.
Darko Jakimovski Level 18, Kriva Palanka, Macedonia, The Former Yugoslav Republic of
22 May 2019
Basically it's a way of putting a No.1 priority on what runs first in your code I guess
Tara Rosenthal Level 18, Farmers Branch, United States
27 April 2020
I think they can overwrite Properties p because that variable is only used as an intermediary to assign a value to namePrefix or maxCatCount.