Results 1 to 2 of 2
- 07-18-2007, 08:31 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 35
- Rep Power
- 0
Help with static variable counter
Hi, i have a pretty easy program, and i wanted to add one function - but it doesn't work. It's a little bit long,
But can you please tell me why doesn't it work?
I highlighted the problematic function. Basically it's supposed to present how many animals i've created, in this case - 4, but it gives me 0 instead.
Java Code:public class Animal { //class variables String Name; int BirthYear, NumOfLegs, AYear=1970; boolean Vegetarian; public static int counter=0; Animal (String theName , int theBirthYear , int theNumOfLegs, boolean theVegetarian) { Name = theName; BirthYear = theBirthYear; NumOfLegs = theNumOfLegs; Vegetarian = theVegetarian; if (BirthYear < AYear) System.out.println( "The animal is old." ); else System.out.println( "The animal is not old." ); } //class methods public void printDetails () { System.out.println( "Name: " + Name ) ; System.out.println( "Year of birth: " + BirthYear ) ; System.out.println( "Number of legs: " + NumOfLegs ) ; System.out.println( "Vegetarian: " + Vegetarian ) ; } public static int addAnimal(){ counter=addAnimal(); return counter++; } public void walk() { System.out.println("The animal is walking in the rain."); } public void run() { System.out.println("The animal is running right now."); } public void eat() { System.out.println("The animal is busy eating"); } }
Java Code:public class MainAnimalDemo { public static void main(String[] args) { //Define & initialize Animal objects : Animal Frog = new Animal ("Crazy frog", 1971, 2, true); Frog.printDetails(); Frog.run(); System.out.println(); Animal Dog = new Animal ("Bobik", 1965, 4, false); Dog.printDetails(); Dog.eat(); System.out.println(); Animal Camel = new Animal ("Moses", 2000, 2, false); Camel.printDetails(); Camel.eat(); System.out.println(); Animal Cow = new Animal ("Cow Angelika", 1954, 4, false); Cow.printDetails(); Cow.walk(); System.out.println("Current number of animals created is: " + Animal.counter); } }
- 07-19-2007, 08:53 PM #2
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
U did add the addAnimal() method in the Animal class, the problem is, u didn't include it inside the constructor
I presume u wanted the counter to add each time u create an animal right?
if so u should add addAnimal(); line inside your constructor
And also,
Java Code:public static int addAnimal(){ counter=addAnimal(); return counter++; }
Java Code:counter=addAnimal()
To increment da animal counter, u don't need this line, it'll bring error toward ur work instead
Good luck with the code :)
Similar Threads
-
non-static member can not be referenced from a static context
By christina in forum New To JavaReplies: 3Last Post: 03-20-2009, 01:35 AM -
Using Static Variables
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 12:08 AM -
Explicit static initialization with the static clause
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 12:07 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 06:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 10:25 PM
Bookmarks