Results 1 to 3 of 3
Thread: Instance variables
- 10-24-2012, 10:50 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
Instance variables
Hello,
I am going through "Java Concepts 6th Edition" by Cay Horstmann and I am having trouble With creating a new counter and a couple of other things. Here is the code from chapter 3.
I think I know why the tally isn't working(this is all the code I have for this problem) but I don't know why the counter isn't working since I did something similar to this in chapter 2.Java Code:import sun.management.counter.Counter; public class counter { private int value = 4; Counter tally = new Counter(); //Error Counter is Abstract; cannot be instantiated tally.count(); //Error package tally DNE tally.count(); //Error packege tally DNE int result = tally.getValue(); //Error Incomparable types required int // found java.lang.Object public void count(){ value = value + 1; } public int getValue() { return value; } }
Can someone please help with thisJava Code:// from chapter 2 JavaApplication63 component = new JavaApplication63(); frame.add(component);
- 10-24-2012, 11:15 PM #2
Re: Instance variables
Why do you have a class called "counter" and a class you import called "Counter"? Java is case sensitive, and class names should never be lower case. I also don't understand how this code even compiles, since you cannot call methods outside of a method body...
- 10-24-2012, 11:29 PM #3
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Instance variables
Well, for staters if Counter is an abstract class, you'll have to create a class that implements Counter, and instantiate that class...
ALL abstract methods in Counter then need to be properly implemented.Java Code:public class MyCounter implements Counter{ .... }
You will instantiate YOUR class...Java Code:@Override public int getFlags() { throw new UnsupportedOperationException(); //or return 0; or return -2000; doesn't really matter //what does matter is that the abstract methods are implemented. }
Java Code:Counter tally = new MyCounter();
Similar Threads
-
instance variables initialization
By ghostrider in forum New To JavaReplies: 6Last Post: 08-27-2012, 07:00 AM -
Instance Variables
By TriSBR in forum New To JavaReplies: 2Last Post: 11-16-2011, 04:16 PM -
Instance variables
By lala in forum New To JavaReplies: 1Last Post: 01-26-2011, 04:38 PM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
static are instance variables
By gabri in forum Advanced JavaReplies: 12Last Post: 09-30-2008, 06:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks