1

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();
 }
asked Feb 1, 2013 at 16:55
8
  • Shouldn't there be a default constructor? Commented Feb 1, 2013 at 16:56
  • When are expected the last two lines are to be called? Commented 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. Commented Feb 1, 2013 at 16:57
  • @sgar91 you only get a default constructor if you don't implement one yourself. Commented Feb 1, 2013 at 16:57
  • 2
    Before getting fancy with classes you should do a few "Hello World" type exercises. Commented Feb 1, 2013 at 17:01

5 Answers 5

7

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();
 }
}
answered Feb 1, 2013 at 16:57

3 Comments

actually first line is fine .Counter tally = new Counter(someintval); this is just an in line initialization )
Right. It would be declaring and initializing a package-protected instance variable named tally. (But that's almost certainly not what was wanted.)
Thanks, I didn't realize Java needed a main method inside of something.
3

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.

answered Feb 1, 2013 at 16:59

Comments

1
Counter tally = new Counter();
tally.count();

is outside of any method, this is the error.

answered Feb 1, 2013 at 16:57

2 Comments

First line is good (except of args, and - as Nathen pointed out - infinite recursion)
Whether or not it is an infinite recursion, depends how these two invalid lines would become compile clean.
0

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);
answered Feb 1, 2013 at 16:57

3 Comments

downvoter comment please .. :) dont tell me that you downvoted me because i dint include main .. :P
upvote from me, to egalize the downvote, and becauee its a correct answer
@AlexWien thanks, it'd have been nice ifthe downvoter left a comment ..:)
0

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

answered Feb 1, 2013 at 16:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.