Results 1 to 3 of 3
  1. #1
    jwl is offline Member
    Join Date
    Jul 2012
    Rep Power

    Default Instance variables


    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.

    Java Code:
    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;
    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:
    // from chapter 2
    JavaApplication63 component = new JavaApplication63();
    Can someone please help with this

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Rep Power

    Default 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...

  3. #3
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Rep Power

    Default 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...

    Java Code:
       public class MyCounter implements Counter{
    ALL abstract methods in Counter then need to be properly implemented.

    Java Code:
    			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.
    You will instantiate YOUR class...
    Java Code:
        Counter tally = new MyCounter();

Similar Threads

  1. instance variables initialization
    By ghostrider in forum New To Java
    Replies: 6
    Last Post: 08-27-2012, 07:00 AM
  2. Instance Variables
    By TriSBR in forum New To Java
    Replies: 2
    Last Post: 11-16-2011, 04:16 PM
  3. Instance variables
    By lala in forum New To Java
    Replies: 1
    Last Post: 01-26-2011, 04:38 PM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  5. static are instance variables
    By gabri in forum Advanced Java
    Replies: 12
    Last Post: 09-30-2008, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts