public class Space {
private int count;
private String varStr;
private int varNum;
private Space(String varStr) {
varStr = varStr;
}
private Space(String varStr, int varNum) {
varNum = varNum;
}
public int getCount() {
return count;
}
public String getVarStr() {
return varStr;
}
public void setVarStr(String varStr) {
varStr = varStr;
}
public int getVarNum() {
return varNum;
}
public void setVarNum(int varNum) {
varNum = varNum;
}
/* toString */
public String toString()
{
String output = "varNum is 1 str is A string ";
return output;
}
/* equals method */
public boolean equals (Object o){
boolean result = false;
Space other;
if (o instanceof Space){
other = (Space)o;
result = getVarNum() == other.getVarNum();
}
return result;
}
/*
DRIVER
*/
public static void main(String[] args) {
System.out.println("Space begin\n");
/* calls the class method */
System.out.println("\tgetCount= " +Space.getCount());
/* calls constructor */
Space q1 = new Space("A string");
Space q2 = new Space("A string", 3);
/* calls the class method again */
System.out.println("\tgetCount= " +Space.getCount());
/* calls toString method */
System.out.println("\ttoString= " +q1);
/* calls your mutator method */
q1.setVarStr("Diff");
q2.setVarNum(5);
/* calls your accessor method */
System.out.println("\tget= " +q1.getVarStr());
System.out.println("\tget= " +q2.getVarNum());
/* calls your equals method */
System.out.println("\tequals= " +q1.equals(q1));
System.out.println("\tequals= " +q1.equals(q2));
System.out.println("\nSpace end\n");
}
}
Refactorings
No refactoring yet !
Vime
November 9, 2007, November 09, 2007 14:49, permalink
public int getCount() is not static, so you can't call Space.getCount() in main..
Instead you can call the getCount() for the Space instances
......
/* calls constructor */
Space q1 = new Space("A string");
Space q2 = new Space("A string", 3);
System.out.println("\tgetCount= " +q1.getCount());
System.out.println("\tgetCount= " +q2.getCount());
......
Mark Webb
November 9, 2007, November 09, 2007 15:00, permalink
Is there a way to change it without changing the constructor?
Mark Webb
November 9, 2007, November 09, 2007 15:17, permalink
A way to make the class work by altering the getCount method without changing the constructor.
Vime
November 9, 2007, November 09, 2007 15:24, permalink
But I didn't change the constructor...
:-?
Marco Valtas
November 10, 2007, November 10, 2007 02:20, permalink
Mark,
I see you are beginning in the Java World, welcome. For this particular issue you can read this short article (http://www.leepoint.net/notes-java/flow/methods/50static-methods.html) about static and non-static methods, this site has a lot information you can use.
Hope this helps.
Pat
November 13, 2007, November 13, 2007 22:11, permalink
Please also take a look at your variable names, they are not wrong but can cause issues See my code
public class Space {
private int count;
private String varStr;
private int varNum;
/* MY Code
private Space(String input)
{
varStr = input;
}
*/
private Space(String varStr) {
varStr = varStr;
}
If anyone can tell me what Ive done wrong here that would be great.
Space.java:69: non-static method getCount() cannot be referenced from a static context