-5
object a = new object(); 
method(object a);

if I change the value of a in this method, the value of a outside of this method also should be changed. But

enum b = enum.something; 
method(enum b);

if I change the value of b in this method, the value of b outside of this method, I found that it didn't change. I don't know why?

JoJo
  • 1,377
  • 3
  • 14
  • 28

3 Answers3

1

enum is not a type, enum is a declarative keyword. Also, parameter types are not declared in method calls, but rather in method declarations. This would be more correct:

public class Main {

    public enum Suit { CLUBS, SPADES, HEARTS, DIAMONDS }

    public static void main(String[] args) {
        Suit suit = Suit.CLUBS;
        print(suit);
    }

    public static void print(Suit suit) {
        System.out.println(suit);
    }
}
Brian
  • 17,079
  • 6
  • 43
  • 66
0
Object a = new Object(); 
method(a);
public void method(Object a){
        // do operation on Object a 
}

Since you are passing the object reference the change will get reflect on your actual object.

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

Enums are constants are meant to keep its state. just like public static final constants.

Philip Puthenvila
  • 512
  • 1
  • 8
  • 19
0

Not sure what you're doing or asking, but here's what I know that seems relevant to your question:

MyObject aOutside = new MyObject(); // 
method(MyObject a) {
  a.setSomeValue("value"); // Change the object referenced by aOutside
  a = new MyObject(); // Does *NOT* change aOutside
}

And

enum MyEnum { AAA, BBB; }
MyEnum  bOutside = MyEnum.something; 
method(MyEnum b) {
  b = MyEnum.AAA; // Does *NOT* change bOutside
}
Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
  • Yes, you got my questions. I just don't know why the second case about enum you give me, that does not change bOutside. @Richard Sitze – JoJo Aug 07 '13 at 04:19
  • See [this relevant SO question](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Richard Sitze Aug 07 '13 at 04:24