0

I am staring to study java and currently I am learning about the classes setters. I see that the most common way to make a setter is something like this.

class Apple{
    private String _name;

    //Setters
    public void setName(String name){
        _name = name;
    }
}

I am used to C so this code raises me a question. If I am setting _name = name in a function, after this function is completed and his stack is discarded why does the variable _name still stores the right value? This is confusing because in C if I assig a pointer to another pointer inside a function like this it would probably cause a segmentation fault (since name is a temporary variable).

Daniel Oliveira
  • 1,280
  • 14
  • 36
  • 2
    You're assigning references to objects. `_name` doesn't refer (or point) to `name`, so it doesn't matter that `name` will be removed. – Tom Sep 25 '16 at 15:24
  • 1
    Java is object oriented, C is not. Java has object instances, when C does not. – Chisko Sep 25 '16 at 15:25
  • use "this" to refer to current object – Khalil M Sep 25 '16 at 15:25
  • Ah right, I forgot that in Java everything is objects and objects are stored in the heap. One more question. If have two variables, a and b both ints and I make b = a, b will be a copy of a, right? Like in C. – Daniel Oliveira Sep 25 '16 at 15:26
  • Java passes variables by value, even though you will hear it's by reference. Check this: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Chisko Sep 25 '16 at 15:29
  • Hint: maybe you should have a good look at the material you are studying. This is a very basic thing in Java; something you actually learn by "studying" ... – GhostCat Sep 25 '16 at 15:53

3 Answers3

0

In Java, you as an user don't have control over the stack as in C/C++ languages.

In addition, all non-primitive data types (int, double, etc.) are stored in the heap. The user is only able to use references or pointers and is not required to perform any kind of memory management, since a mechanism known as garbage collector already frees those instances which haven't any reference to them. Therefore, there are no such thing as pointers in Java, although you can assign a null value to a non-primitive variable: Foo f = null;

Therefore, in Java you could literally behave what this C++ code does:

class Foo {
    Foo( int a ) {}
};

void bar( int a ) {
   Foo f(a); // f is placed in the stack
}

The only way you can create an instance of Foo in java would be like this:

void bar( int a ) {
   Foo f = new Foo(a); /* f is a reference to the new instance
                        * (placed in heap) */
}
Jorge Bellon
  • 2,901
  • 15
  • 25
0

Actually, name is an instance variable and setName() is an instance method. This means that there is a separate copy of name for every object of the class Apple. The setName() method sets the value for the global variable of the object it is called with to that of its argument. So, even after the stack of the method is discarded, the object exists and so does the value of this object's copy of name. See this example below:

class Apple {
    private String name; //instance variable

    public void setName(String name) {
        this.name = name; //same as what you have written
    }

    //main method
    public static void main(String[] args) {
        Apple obj = new Apple(); //object created
        Apple obj2=new Apple();
        obj.setName("Yolo");
        obj2.setName("Yay!");
        System.out.println(obj.name); //displays obj's copy of name that holds the value "Yolo"
        System.out.println(obj2.name); //displays obj2's name
    }
}

This displays

Yolo
Yay!

I hope this makes it clear to you.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
progyammer
  • 1,498
  • 3
  • 17
  • 29
-3

You are setting the reference of name to the reference of _name so they look in the same spot in memory. Therefore when the instance of setName disappears and the variable name with it the reference stored in _name remains.

Since you set _name to private it can only be accessed inside the class Apple. You cant change it within your main method and that's why you create the set method so it can be changed by outside classes (I.e. Your main method)

Kenny Hammerlund
  • 436
  • 4
  • 14
  • 1
    `_name = name` is completely valid... You also don't answer the question. – Andrew Li Sep 25 '16 at 15:32
  • I guess the better answer is that you are setting the reference of name to the same as the reference of _name. – Kenny Hammerlund Sep 25 '16 at 15:44
  • Edited: hopefully it's more helpful – Kenny Hammerlund Sep 25 '16 at 15:50
  • 1
    *"when the instance of setName disappears"* this is still incorrect. A method is not an object and the object where the method has been called on, doesn't disappear. What you mean is the situation where the program flow exits the method, due to completion. – Tom Sep 25 '16 at 16:16