CodeGym /Courses /Java Core /Tasks on the order of variable initialization

Tasks on the order of variable initialization

Java Core
Level 5 , Lesson 9
Available

"Hello, Amigo!"

4
Task
Java Core, level 5, lesson 9
Locked
Static modifiers: part 1
In a static block, initialize labels with 5 different key-value pairs. The Solution class should have only one method (main), where you must also declare a static Map field labels. This field must be populated with 5 different key-value pairs in a static block.
8
Task
Java Core, level 5, lesson 9
Locked
Static modifiers: part 2
In a static block, read two int variables A and B from the console. Don't forget about the IOException that must be handled in the catch block. Finally, use the close() method to close the input stream.
8
Task
Java Core, level 5, lesson 9
Locked
Default values
In this task, you need to create 7 public fields for a class. Be sure they are initialized with default values ​​(int intVar, double doubleVar, Double DoubleVar, boolean booleanVar, Object ObjectVar, Exception ExceptionVar, and String StringVar). In the main method, display their values in the specified order.
8
Task
Java Core, level 5, lesson 9
Locked
Static modifiers and exceptions
Let's throw some exceptions! In a static block, throw an Exception so that the class doesn't load and you see an error message instead of the value of the variable.
4
Task
Java Core, level 5, lesson 9
Locked
Static modifiers and kittens
Let's introduce cats to the world of static blocks. First, in the Solution class, we'll create a Cat class with a public String field name, but without a constructor with parameters. Then in a static block, we'll create a Cat object and display the created cat's name on the screen.
Comments (27)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
ImDevin Level 15, Old Town, United States
22 June 2021
I have general understanding of static block, but I wonder how much it gets used in a real production code settings. 🤔
Jonaskinny Level 25, Redondo Beach, United States
2 March 2022
In framework code its used usually once in each class that needs to pull state on initialization ... so things like connection/object pools use it all the time.
Oliver Heintz Level 18, Mustang, United States
16 January 2021
I think I understand about 50% what they expect me to with these tasks.
Hoa Nguyen Level 15, Australia Expert
8 February 2022
Me too. Quite complicated!
Chandan Thapa Level 22, Dubai, United Arab Emirates
3 December 2020
i was lost in a silly mistake in the last question - the task asks to initialize the cat variable in the static block. I was initializing like this => Object obj = new Object(); and it should be obj = new Object(); I know its stupid. hope it helps if any of you faced the same.
Vitalina Level 20, Poland
14 December 2020
it's not stupid:) Of course, usually we initializing Object obj = new Object(); So it would be in our task: Cat cat = new Cat(); But we're already have a variable Cat cat. That's why we just assign a new Cat() to this variable. cat = new Cat(); I hope you understand it:) Let me know if you'll need any help:)
Chandan Thapa Level 22, Dubai, United Arab Emirates
15 December 2020
yes crystal clear! haha thanks :)
Amo programmare Level 32, Italy Expert
15 February 2023
I did the same mistake, but then it made me wonder in the case of typing Cat cat = new Cat() versus cat = new Cat() would that mean that in the first case we are creating a complety different/new cat variable? Or would we still be using the same cat variable as created in: public static Cat cat; ? I'm just trying to understand if it makes a difference just in the CodeGym verifyer or in the real world.
Michael Brimage Level 19, Washington D.C., United States
17 April 2020
Can someone explain why we nee static blocks? I am failing to see the utility in this concept. this lack of understanding hinders my ability to solve the problems.
B Sangappa Level 18, Bangalore, India
26 April 2020
Hi Michael, Static block is like a method but has the highest precedence than the Class's constructor. The static block gets executed only once even if you create multiple objects of that class. And in majority cases, static blocks used to assign or initialize static variables.
Vidhya Level 16, Sunnyvale, United States
1 June 2020
Basically we can think of static block as unit of code which needs to initialised even before any objects of the class is created . Lets say i have a static variable in my class the value of that variable can be initialised like below . When we define as a static variable , it will created when the class is loaded , even though no instances of the class is created . (Ideally when the class is referred some where this value is initialized. so we can directly access them with ClassName. variable name). Static block allows error handling also). The below code will help in initialising the path to be used by all instances of the class private static String filePath = "C:\logs"; or private static String filePath; static{ filePath = "C:\logs"; }
Manish Sinha Level 26, london, United Kingdom
12 April 2020
Hello Codegym Team, Again thanks for making this beautiful course available to us. just a quick feedback: for task: 1517: it should be hinted that only RuntimeException could be used. I just got to know that RuntimeException does not need try and catch block for this program. Bit interesting. regards, Manish
Tara Rosenthal Level 18, Farmers Branch, United States
27 April 2020
It also works if you use an if statement with other types of exceptions: if(true) throw new ExceptionInInitializerError(); Basically, you have to trick the IDE to not just prevent the code from compiling due to an obvious error.
Ntuthuko Xaba Level 18, Johannesburg, South Africa
11 April 2020
Will somebody please help me with ' task1515 '
Manish Sinha Level 26, london, United Kingdom
12 April 2020
what's ur issue?
Ntuthuko Xaba Level 18, Johannesburg, South Africa
13 April 2020
Tnx ,its resolved
Robert Constantinescu Level 25, Bucharest, Romania
21 November 2019
Basically what i have understood here is that anything that have static and is not in main, they will be initialized from top to the beginning. So you should put them in the order you want to be initialized, either variables or methods
Alexandre Lalancette Level 41, Quebec, Canada
15 November 2019
This is still "abstract" for me
Koniasz Level 22, Warszawa, Polska
25 August 2019
Should I go back in course if it all stops being so obvious as in the begining?
16 August 2019
Last one took some time .. Is it all clear ... not sure