I am trying to make use a class I made, but when I call the class is doesn't work and I am kind of lost.
The error is coming at the class call and initialization of tally at the bottom of the program.
package counter;
public class Counter {
private int value;
public Counter(int intialValue){
value = intialValue;
}
private void count() {
value = value + 1;
}
private void reset(){
value = 0;
}
public int getValue(){
return value;
}
Counter tally = new Counter();
tally.count();
}
-
Shouldn't there be a default constructor?sgarizvi– sgarizvi2013年02月01日 16:56:41 +00:00Commented Feb 1, 2013 at 16:56
-
When are expected the last two lines are to be called?Mordechai– Mordechai2013年02月01日 16:57:13 +00:00Commented Feb 1, 2013 at 16:57
-
You can't have program calls like that within the class(they arent in a method) You need to move that code to a main method that runs on start up.jzworkman– jzworkman2013年02月01日 16:57:18 +00:00Commented Feb 1, 2013 at 16:57
-
@sgar91 you only get a default constructor if you don't implement one yourself.twain249– twain2492013年02月01日 16:57:24 +00:00Commented Feb 1, 2013 at 16:57
-
2Before getting fancy with classes you should do a few "Hello World" type exercises.Hot Licks– Hot Licks2013年02月01日 17:01:01 +00:00Commented Feb 1, 2013 at 17:01
5 Answers 5
All Java statements must be put into a method of some kind.
Currently your last two lines are not in a method.
Counter tally = new Counter();
tally.count();
Try something like this:
public class Counter {
... existing members ...
public static void main(String[] args) {
int initialValue = Integer.parseInt(args[0]);
Counter tally = new Counter(initialValue);
tally.count();
}
}
3 Comments
Counter tally = new Counter(someintval);
this is just an in line initialization )tally
. (But that's almost certainly not what was wanted.)From what you've shown so far, it looks like your class Counter has a Counter as an instance member, so that you have an infinite regression trying to instantiate this. You don't give the error but I'd expect a StackOverflowError to result.
That assumes it compiled, which shouldn't happen because the line tally.count() should not be legal. The only things that go into a class are constructor declarations, method declarations, variable declarations, initializer blocks, and nested inner class declarations. The code at the bottom of your class doesn't count as any of those.
Also if you include a constructor with arguments then if you want to call a zero-arg constructor you have to create one explicitly. The code calling the nonexistent zero-arg constructor will cause another compiler error.
So you have a misunderstanding about constructors, plus confusion about what it means to declare things within a class.
Comments
Counter tally = new Counter();
tally.count();
is outside of any method, this is the error.
tally.count();
should be inside a method body .
public void someMethod() {
tally.count();
}
Also, compiler will not include a default no-args constructor in your class, as you have already written an 1-arg constructor so you will have to pass a valid int value to your constructor.
Counter tally = new Counter(someintval);
3 Comments
You haven't passed a value into the instance of the class:
Counter tally = new Counter(10);
Or maybe it's because it is not inside a
public static void main(String args)
method body