Some help

  • Thread starter horvath.alexandru
  • Start date
H

horvath.alexandru

Hello.

I have the following singleton:

public class Singleton {


private static final Singleton singleton = new Singleton();

private ArrayList<String> list = new ArrayList<String>();

private Singleton() {
}

public static Singleton getInstance() {
return singleton;
}


// ..... and some public methods to access "list";


}

..... and i have the following class:


public class Usage {

private final Singleton singletonInstance =
Singleton.getInstance();

}



My question is: it is ok that in Usage class the singletonInstance is
declared final? can i still add/delete from the list declared in the
Singleton? Why?

Thank you in advance
 
M

Matt Humphrey

Hello.

I have the following singleton:

public class Singleton {


private static final Singleton singleton = new Singleton();

private ArrayList<String> list = new ArrayList<String>();

private Singleton() {
}

public static Singleton getInstance() {
return singleton;
}


// ..... and some public methods to access "list";


}

.... and i have the following class:


public class Usage {

private final Singleton singletonInstance =
Singleton.getInstance();

}



My question is: it is ok that in Usage class the singletonInstance is
declared final? can i still add/delete from the list declared in the
Singleton? Why?

The final applies to the reference to the Singleton instance, not to its
contents. It simply prevents you from changing to a different Singleton
instance.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
H

horvath.alexandru

I understand now :) .
I'm new to Java so i am sorry if my question was a little silly.
Thank you for your quick response.
 
J

Jinkx

It was not at all silly., it was a very good question . Even i had
little idea about that...
 
R

Roedy Green

private final Singleton singletonInstance =
Singleton.getInstance();

}



My question is: it is ok that in Usage class the singletonInstance is
declared final? can i still add/delete from the list declared in the
Singleton? Why?

Why would final not be ok? You don't change the value anywhere do you?

Final is not C++ const. It simply means the reference to the object is
frozen. It says nothing about changing the value of fields in the
object. It says there is one object for all time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top