I have a java class:
class Card {
private static Card instance = new Card();
public int value:
private Card() {}
public static Card sharedInstange() {
return instance;
}
}
This class models a shared instance which can be access by different threads. The instance variable value therefor must be synchronized such that only one thread can read and write it at a time.
How can I achieve that my instance variables of the Card class can only accessed by one thread at the time?
Edit: Here is the same class with getter and setter.
class Card {
private static Card instance = new Card();
private int value:
private Card() {}
public static Card sharedInstange() {
return instance;
}
public int getValue() { return value; }
public void setValue(int v) { this.value = v; }
}
Make it volatile and create getters/setters without synchronized keyword.
Or just make it AtomicInteger.
Related
When one object of a class has a reference to another object of
the same class, the first object can access all the second object’s
data and methods (including those that are private).
I took this sentence from a book. But I couldn't figure out actually what it means.
It means that private members are visible to other instances of the same class. For example:
class A {
private int v;
public boolean isSameV(A other) {
return this.v == other.v; // can acccess other.v
}
}
It means that if you have a class that looks like this
public class A {
private int number;
private A otherInstance;
public int number2;
public void DoStuff() {
...
}
}
you can access A.number in the DoStuff method (or any other class method) even although number is actually private.
e.g.
public class A {
...
public void DoStuff() {
this.otherInstance.number = 42;
^^^^^^^
cannot access private members here
}
}
is perfectly fine, while
public class B {
private A aInstance;
public void DoStuffToo() {
this.aInstance.number = 42;
}
}
would not compile, because B cannot access A's private members.
Good question actually, I faced similar problem when I started learning Java, here is how it looks in practice:
public class A {
private String example;
protected int anotherOne;
public A(){
}
public A(A a){
this.example = a.example; // here we get access to private member of another object of same class
this.anotherOne = a.anotherOne; // it works for protected as well
}
// This works for methods not just constructor, lets consider we want to swap value of example:
public void swapExample(A a){
String temp = a.example;
a.example = this.example;
this.example = temp;
}
}
Private fields can be accessed from inside of the class, by this construction you can access all the field of an instance of Foo without getters and setters when you are in class Foo :
public class Foo {
private String name;
public int sumLetter(Foo b) {
return this.name.length() + b.name.length();
}
}
The doc : Declaring Member Variables :
private modifier — the field is accessible only within its own class.
I got the object wolfOne of the Class Wolf, and I need to access to its variable mAlive in another Class, how may I don it?
Wolf wolfOne;
//Wolf Class
public class Wolf extends Card {
public Wolf(){
mCharacter = "Wolf";
}
public void savage(Card card) {
card.mAlive = false;
}
}
//Card Class
public class Card {
//Names
public String mCharacter;
//Status
public static boolean mAlive;
public static boolean mDefended;
public static boolean mOwled;
public static boolean mLastSavaged;
public static boolean mLastLynched;
//Constructor
public Card() {
// Do Card specific stuff.
}
}
Remove static from all of your Class variables - make them instance variables instead. Then provide typical getters/setters for each, allowing clients of the class to retrieve or mutate the value:
public class Wolf extends Card {
public Wolf(){
setMCharacter("Wolf");
}
public void savage(Card card) {
card.setMAlive(false);
}
}
public class Card {
//Names
private String mCharacter;
//Status
private boolean mAlive;
private boolean mDefended;
private static boolean mOwled;
private static boolean mLastSavaged;
private static boolean mLastLynched;
public String getMCharacter(){}
return mCharacter;
}
public void setMCharacter(String value){
this.mCharacter = value;
}
public boolean getMAlive(){
return mAlive;
}
public void setMAlive(boolean alive){
this.mAlive = alive
}
//....So on and so forth
}
static has a special meaning in Java. It doesn't mean that the variable or method is inheritable; it means that there is only one of it that belongs to the class, not the instance.
To inherit from a super class, all that is required is that it not private and the inheriting classes will get it. The following example shows this relationship.
import java.util.*;
import java.lang.*;
import java.io.*;
class A
{
public String name;
public boolean isAlive;
public A()
{
name = "A";
isAlive = true;
}
}
class B extends A
{
public B()
{
name = "B";
isAlive = false;
}
}
public class Main
{
public static void main (String[] args)
{
A a = new A();
A b1 = new B();
B b2 = new B();
b2.name = "B2";
b2.isAlive = true;
System.out.println(a.name);
System.out.println(a.isAlive);
System.out.println(b1.name);
System.out.println(b1.isAlive);
System.out.println(b2.name);
System.out.println(b2.isAlive);
}
}
And gives this output:
A
true
B
false
B2
true
This can be run here.
In the card class make the fields private not public, in oo this is called encapsulation or data hiding (look it up). Then simply add a getMAlive method that returns the mAlive value and a setMAlive method which will set it. Now in your wolf class to set mAlive you can with setMAlive(boolean). For external objects you will need to have a reference to your wolf/card and call wolfName.getMAlive()
For card...
private boolean mAlive;
public boolean getMAlive(){
return mAlive;
}
public void setMAlive(boolean value){
mAlive = value;
}
For wolf...
public void savage(){
setMAlive(false);
}
For other classes to get mAlive...
wolfName.getMAlive()
You may consider making your mAlive (and other fields in Card) protected. Protected fields can only be seen by those classes that extend them e.g. wolf. So in wolfs savage method you could do...
public void savage(){
mAlive = false;
}
But to set mAlive from other classes you would still need a setter in Card so yeah
I hope this helps :) good luck
I have a static method which needs to invoke the SportsMenComparator. But this, as we all know is not allowed. How does a static function use a comparator subclass ? Although I have workarounds, I am looking for best practices for this particular problem.
final class SportsMan {
private final String name;
private final int rank;
private final String sport;
public SportsMan (String name, int rank, String sport) {
this.name = name;
this.rank = rank;
this.sport = sport;
}
public String getName() {
return name;
}
public int getRank() {
return rank;
}
public String getSport() {
return sport;
}
}
final class Sport {
private final String sport;
private final int numberOfPlayers;
public Sport(String sport, int numberOfPlayers) {
this.sport = sport;
this.numberOfPlayers = numberOfPlayers;
}
public String getSport() {
return sport;
}
public int getNumberOfPlayers() {
return numberOfPlayers;
}
}
public final class Joins {
private Joins () {}
public class SportsMenComparator implements Comparator<SportsMan> {
#Override
public int compare(SportsMan s1, SportsMan s2) {
return s1.getSport().compareTo(s2.getSport());
}
}
public static void innerJoinSort(List<SportsMan> sportsMans, List<Sport> sportList) {
Collections.sort(sportsMans, new SportsMenComparator());
}
}
Eclipse results in the following message: No enclosing instance of type Joins is accessible where Joins is name of the enclosing class.
But this, as we all know is not allowed. How does a static function use a comparator subclass ?
You cannot use a non static reference,still you are allowed to create a new object and use it. So since you are creating a new SportsMenComparator object and passing, no issues.
For example:
public static void main(String[] args) {
List<String> s =new ArrayList<String>();
s.add(""); // allowed
}
But
List<String> s =new ArrayList<String>();
public static void main(String[] args) {
System.out.println();
s.add(""); // Error: Cannot make a static reference to the non-static field s
}
Edit:
Since you defined the comparator class inside the Joins , you need the Joins object to access the comparation inside it
Collections.sort(sportsMans, new Joins().new SportsMenComparator());
For using a Comparator, there is no difference between using it from a static- or non-static method. In either case an instance of the Comparator has to be used.
The Garbage Collector of modern JVMs is very efficient at handling short-lived objects. Therefore the penalty to be paid for using a fresh instance (via new) every time is usually no issue. However, if you don't want to use a fresh instance every time, I think the best option would be to add a static field to your SportsMenComparator, containing a singleton instance of the comparator:
public class SportsMenComparator implements Comparator<SportsMan> {
public static final SportsMenComparator instance=new SportsMenComparator();
#Override
public int compare(SportsMan s1, SportsMan s2) {
return s1.getSport().compareTo(s2.getSport());
}
}
public static void innerJoinSort(List<SportsMan> sportsMans, List<Sport> sportList) {
Collections.sort(sportsMans, SportsMenComparator.instance);
}
The problem is that you try to access an instance element (in this case it is a class, indeed the same as with a filed or method) within a static method, which is not associated with an instance. SURESH ATTA's answer is right, but you can also make your SportsMenComparator class static and it will work. I do not sse any reason to associate your comparator with an instance of the Joins class.
One can use something like this---
public static boolean someMethod(MyObject obj1, MyObject obj2){
return obj1.compare(obj2);
}
Why you cant include parameter to the function.
public static void innerJoinSort(List<SportsMan> sportsMans, List<Sport> sportList, Comparator comparator) {
Collections.sort(sportsMans, comparator);
}
I have a Singleton class to save the state of an application's module.
This class simply have a lot of class variables with setters and getters :
public class ModuleState{
private static ModuleState instance;
private A a;
private B b;
private C c;
..
..
..
..
private ModuleState (){}
public ModuleState getInstance(){
if(instance==null)
instance=new ModuleState();
return instance;
}
}
At a precise moment of the application lifecycle, i have the need to CLEAR the module's state. What i do now is to reset ALL the variables in ModuleState by a clearAll() method like this:
public void clearAll(){
a=null;
b=null;
c=null;
..
..
}
My question is the following : there is a cleaner method to do this reset? Possibly clearing the singleton instance itself, without resetting every class variable?
The problem with this approach is that i may have the need to add a new class variable to the ModuleState. In this case i must remember to add a line in the clearAll() method to reset the new variable.
What about ...
public static volatile ModuleState instance = null;
public static void reset() {
instance = new ModuleState();
}
p.s.: as per discussion below: in a multithreaded environment it's very important to synchronize the access on the instance because the JVM is allowed to cache its value. You can use volatile as shown above. Thanks to all!
Cheers!
no, this approach is perfectly acceptable. you are of course synchronizing access to these state objects in some way, right? otherwise you risk someone seeing a half-cleared config object.
another thing you could do to future-proof yourself against any extra state added in the future is store all of your state in a HashMap, for example, instead of individual fields. this way, clear()ing the hashmap ensures that all state is wiped and adding any extra state in the future becomes safer
You need to maintain the same object instance, in order to comply with the Singleton pattern, so your approach makes sense: altering the members.
However, if you wanted to clean it up a little bit, why not just have an internal list, like:
ArrayList<Object> members = new ArrayList<Object>();
// If it actually is Object, there's no need to paramaterize.
// If you want, you can actually make the members implement a common interface,
// and parameterize the ArrayList to that.
Another Option would be to have a HashMap, that binds the key word to the member.
HashMap<String,Object> members = new HashMap<String,Object>();
// Again, same parameterization rules apply.
For an ArrayList or a HashMap, the clearAll method might look like this:
public class ModuleState()
{
public void clearAll()
{
members.clear();
}
}
This method won't need to change.
May be this can help you:
public class SingletonBean {
private static SingletonBean instance = new SingletonBean();
private static Object privateMutex = new Object();
private SingletonBean() {
//to prevent instantiation
}
public class ObjectsContainer {
private Object A;
private Object B;
private Object C;
public Object getA() {
return A;
}
public void setA(Object a) {
A = a;
}
public Object getB() {
return B;
}
public void setB(Object b) {
B = b;
}
public Object getC() {
return C;
}
public void setC(Object c) {
C = c;
}
}
private ObjectsContainer objectsContainer;
private void resetObjectsContainer() {
objectsContainer = new ObjectsContainer();
}
public static SingletonBean getInstance() {
return SingletonBean.instance;
}
public static void clearAll() {
synchronized (privateMutex) {
SingletonBean.getInstance().resetObjectsContainer();
}
}
public static ObjectsContainer getObjectsContainer() {
synchronized (privateMutex) {
return instance.objectsContainer;
}
}
}
public class SomeClass {
public void someMethod() {
SingletonBean.getObjectsContainer().getA();
}
}
Make an inner class to hold the fields, then replace that instance when you want to reset. The write to the field would make the change to all three fields essentially atomic.
public class ModuleState {
private static volatile ModuleState instance;
private static class Values {
A a;
B b;
C c;
}
private volatile Values values = new Values()(
private ModuleState (){}
public ModuleState getInstance(){
if (instance==null) {
synchronized (ModuleState.class) {
if (instance==null) {
instance = new ModuleState();
}
}
}
return instance;
}
public synchronized A getA() {
return values.a;
}
public synchronized void reset() {
values = new Values();
}
By the way, your null checking initialization code was not threadsafe. I fixed that too.
Note that to make this work, you must make the reference to values volatile and synchronize all access to it, otherwise (due to the java memory model) other threads than the one that calls reset() may see the old reference.
I'm developing an application for Android, and at one point the user chooses a region, after which the contents of all parts of the app is changed. Therefore I need to access an integer throughout the program. I have considered a singleton class, but I can't figure out how to add just an int, a get() and a set() to it (I want to be able to read everywhere and write in two classes(everywhere is fine)).
Should I simply declare it global?
This is what I've got going now, is it ok?
public enum Region {
INSTANCE;
private int rID =0;
public void setRID(int rID) {
this.rID=rID;
}
public int getRID()
{
return rID;
}
}
To be accessed with
Region.INSTANCE.setRID(5);
Try something like
public class State
{
static State instance = new State ();
public static State getInstance() { return instance; }
private int value;
public void setValue (int value) { this.value = value; }
public int getValue () { return value; }
}
Don't forget to store your value when your application exits, and restore it when it reloads.
This is not really a Singleton, but a static class. A class that contains a static field (a field not bounded to an instance):
public class StaticClass {
private static int value;
private Singleton () {}
public static int getValue () {
return value;
}
public static void setValue (int val) {
value = val;
}
}
you can access the value by StaticClass.getValue() and set the value by StaticClass.getValue(4). ('StaticClass must not be replaced by an object).
The private constructor prevents one to create an instance by accident.
Create a public static class and declare the setter and getter as public.
I went with an enum:
public enum Region {
INSTANCE;
private int rID =0;
public void setRID(int rID)
{
this.rID=rID;
}
public int getRID()
{
return rID;
}
}
Which I access with
Region.INSTANCE.getRID();