E41e6fdad12edcfb97ab662e83bfb9ab

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 !

90ec841f866ac35c4ce90ee3cfffbdc3

Vime

November 9, 2007, November 09, 2007 14:49, permalink

No rating. Login to rate!

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

1
2
3
4
5
6
7
8
9
10
11
12
......

/* 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());


......
E41e6fdad12edcfb97ab662e83bfb9ab

Mark Webb

November 9, 2007, November 09, 2007 15:00, permalink

No rating. Login to rate!

Is there a way to change it without changing the constructor?

90ec841f866ac35c4ce90ee3cfffbdc3

Vime

November 9, 2007, November 09, 2007 15:07, permalink

No rating. Login to rate!

I don't understand, what do u mean?

E41e6fdad12edcfb97ab662e83bfb9ab

Mark Webb

November 9, 2007, November 09, 2007 15:17, permalink

No rating. Login to rate!

A way to make the class work by altering the getCount method without changing the constructor.

90ec841f866ac35c4ce90ee3cfffbdc3

Vime

November 9, 2007, November 09, 2007 15:24, permalink

No rating. Login to rate!

But I didn't change the constructor...
:-?

B8d457d2c39911ea4c74ba7d66b9c3f7

Marco Valtas

November 10, 2007, November 10, 2007 02:20, permalink

No rating. Login to rate!

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.

Avatar

Pat

November 13, 2007, November 13, 2007 22:11, permalink

No rating. Login to rate!

Please also take a look at your variable names, they are not wrong but can cause issues See my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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; 
      }

Your refactoring





Format Copy from initial code

or Cancel