Is it possible to call a final object in Java?

u2m2 :

Is it possible to call a final object which is declared in a method, in another method of the same class?

public class MyClass{

    public void description(){
        //something...
        //something..

        final HashMap<String,String> desc = new HashMap<String,String>();
    }

    public void call(){

    }
}

Can I call the object desc in function call()?

Dmitry Pisklov :

There's no such thing as final object in java. What you declared is a final variable, or final reference. It means you cannot reassign this variable name within the scope. This says nothing about the object being referenced however. So you can pass this object as follows:

public class MyClass {

  public void description() {
    //something...
    //something..

    final HashMap<String,String> desc = new HashMap<String,String>();
    call(desc);
  }

  public void call(Map<String, String> desc) {
    // do something, may be desc.put(key, val);
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=296512&siteId=1