CodeGym /Courses /Java Core /Order of variable initialization

Order of variable initialization

Java Core
Level 5 , Lesson 8
Available

"Hello, Amigo! Today, Bilaabo will talk about the order in which variables are initialized."

Imagine you're looking at some code. What values do the variables get?

Code
class Cat
{
 public int a = 5;
 public int b = a + 1;
 public int c = a * b;
}
Code
class Cat
{
 public int a = getSum();
 public int b = getSum() - a;
 public int c = getSum() - a - b;

 public int getSum()
 {
  return a + b + c;
 }
}

"Is that really allowed?"

"Of course. The order in which a class's member methods and fields are declared is not important."

A class is loaded from top to bottom, so it's important that a field only accesses other fields that have already been loaded. In the example, b can access a, but it doesn't know anything about c.

"And what will happen?"

"When variables are created, they get default values."

Code What really happens
class Cat
{
 public int a = 5;
 public int b = a + 1;
 public int c = a * b;
}
class Cat
{
 public int a = 0;
 public int b = 0;
 public int c = 0;

 public Cat() { super();

 a = 5;
 b = a + 1; //5+1 = 6
 c = a * b; //5*6 = 30
 }
}
class Cat
{
 public int a = getSum();
 public int b = getSum() - a;
 public int c = getSum() - a - b;

 public getSum()
 {
  return a + b + c;
 }
}
class Cat
{
 public int a = 0;
 public int b = 0;
 public int c = 0;

 public Cat() { super();

  a = getSum(); //(a+b+c)=0
  b = getSum() - a; //(a+b+c)-a=b=0
  c = getSum() - a - b; //(a+b+c)-a-b=c=0
 }

 public getSum()
 {
  return a + b + c;
 }
}

"Holy moly! It's so simple. Thank you, Bilaabo. You're a real friend!"

"Hooray! Bilaabo has a friend!"

Comments (10)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Tasmoda Level 28, Midrand, South Africa
20 June 2022
Great article!
TheLordJackMC Level 39, Princeton, idk somewhere
17 July 2021
i thought this guy was languageist but he stopped talking about pascal
Andrei Level 41
7 December 2020
My question is: do methods initiate before primitives? For example, in this case, getSum() method is loaded before the variables? Because it is declared after them.
Clint Level 22, cincinnati, united states
26 November 2020
it would be nice to standardize having the outputs from the code shown in all examples
Thomas Sixberry Level 16, Rochester Hills, United States
14 February 2020
They should of been explaining things this way from the beginning..
Joy Majumdar Level 16, Kolkata, India
7 August 2019
So default values for numbers are zero(whole number) ?
Andrei Level 41
7 December 2020
Yes, you can google the default values for every type of primitive. String is NULL, int is 0. double is 0.0d, float is 0.0f etc
Tangerin Level 27, Chongqing, China
11 July 2019
I think this level shall be put in the front of OOP levels. It doesn't consist much memory-requiring knowledge and I will be glad to know all of that before the OOP starts, it helps me clear everything, just like what every language gots, grammar.
Henk Level 19, Pretoria, South-Africa
22 May 2019
isn't this logical ? a, b and c have been assigned 0 as value, so won't add up to anything.... Sorry, not trying to be a wise-ass, but the example is not clear. In fact, if you copy and paste this into your main method, it doesn't even compile. As you cannot call a function (or method) to calculate variables that are only declared after the function call ? public int a = getSum(); //here it already tries to add b and c to a, but b and c still have to be declared public int b = getSum() - a; public int c = getSum() - a - b; public getSum() { return a + b + c; }
Greggics Level 20, Frankfurt am Main, Germany
22 May 2019
The example is missing the return type in the method signature. Change it to

public int getSum()
and the code will work just fine :) What I can take from this lesson, is that a variable isn't allowed to access another variable that is later declared inside the class. However you can use a method as a work-around, since it doesn't matter where in the class said method was declared. But that makes me wonder: Why is int a directly accessing int b prohibited? All variables are created with default values. And only later in the constructor the intended values will be assigned. And by then all variables are already known. So why prohibit this access in the first place? Is it simply to prevent a potentially (for the user) unexpected behavior and or is there more to it?