-1

Couldn't find an explicit description of what's happening so thought i'd bring this up to the community.

public class Temp {
static int i;
int j;
int sum = i+j;
}


public class Main{
public static void main(String[] args){

Temp obj = new Temp();
obj.i = 1;
obj.j = 2;
System.out.println(obj.sum); //returns '0'

}}

Is it because both integers i and j were empty during instantiation that the 'sum' variable is empty?

Thanks in advance!

  • 1
    See [*ยง4.12.5. Initial Values of Variables*](http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.5). โ€“ trashgod Apr 09 '17 at 02:50
  • 1
    "Empty" or more precisely they were 0. Java primitives are never "empty," they are initialized to 0 (or false). References can be `null` which is not really "empty" either, null is a value. โ€“ markspace Apr 09 '17 at 02:52
  • 1
    To restate what markspace said. There is no such thing as an "empty" variable or field in Java. Every variable or field has a *definite value*, any time that you can observe it. That value is the result of either a definite initialization (e.g. `int foo = 42;`) or an implicit *default initialization*. Remove "empty variable" from your mental model, and your vocabulary! โ€“ Stephen C Apr 09 '17 at 03:37

3 Answers3

1
Temp obj = new Temp(); // creates an instance of object type Temp

Here, data members, i, j, and sum are initialized to 0

obj.i = 1; // assigns value of Temp data member, i to 1
obj.j = 2; // assigns value of Temp data member, j to 2

Note that the value of data member sum of Temp Object obj is still 0.
To make, sum = i + j, you need to initialize it to i + j when i and j are initialized.

Simply write obj.setSum() method to set the value of sum and obj.getSum() after that to retrive the updated value of it.

public class Temp {
    static int i;
    int j;
    int sum = i+j;

    public void setSum(){
        sum = i + j;
    }
    public int getSum(){
        return sum;
    }

}


public class Main{
    public static void main(String[] args){

    Temp obj = new Temp();
    obj.i = 1;
    obj.j = 2;
    obj.setSum();
    System.out.println(obj.sum); //OR obj.getSum()

    }
}
Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27
0

Is it because both integers i and j were empty during instantiation that the 'sum' variable is empty?

Yes, i+j is assigned to 'sum' when the Object is instantiated. By default java assign 0 to int values when you don't assign a value.

You need to update the sum variable by directly assigning the value to it.

obj.i = 1;
obj.j = 2;
obj.sum = obj.i + obj.j.

A workaround is to create a getter method in your Temp class instead of the variable sum:

public class Temp {

    static int i;
    int j;

    public int getSum() {
        return i + j;
    }
}

Then to print the sum :

System.out.println(obj.getSum());
Flood2d
  • 1,338
  • 8
  • 9
0

When you create another class that will be use by the main method

numeric data fields are set to zero

Character fields are set to Unicode \u0000

Boolean fields are set to false

Fields that are object references are set to null or (empty) for example String data fields

0xDEADBEEF
  • 590
  • 1
  • 5
  • 16