Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Now i have some classes as below:
C#
public class A {
	private static A a = new A();
	private int num1 = 0;
	private int num2 = 0;
	public int getNum1() {
		return num1;
	}
	public void setNum1(int num1) {
		this.num1 = num1;
	}
	public int getNum2() {
		return num2;
	}
	public void setNum2(int num2) {
		this.num2 = num2;
	}
	private  A(){}
	public static A getInstance(){
		return a;
	}
}
public class B {
	private A a = A.getInstance();
	public void setNum1(){
                //some codes was executed and get the result
		a.setNum1(result);
	}
	public void setNum2(){
               //some codes was executed and get the result
		a.setNum2(result);
	}
	public A getA() {
		return a;
	}
	public void setA(A a) {
		this.a = a;
	}
}
public class C {
	public static void main(String[] args) {
		B b = new B();
		A a = b.getA();
		b.setNum1();		//line 1
		b.setNum2();		//line 2
		int num1 = a.getNum1();//line 3
		int num2 = a.getNum2();//line 4
		System.out.println(num1);
		System.out.println(num2);
	}
}

I just want use object a to get the values in class C,why I must use line 1 and 2?
How do i design when delete line 1 and 2 that i also can get the truly values?
Posted

1 solution

You could set values in the constructor to avoid using the SetNum() method.

C#
public class B {
	private A a = A.getInstance();
	B(){
                //some codes was executed and get the result
		a.setNum1(result);
	}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900