Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2007, 08:31 PM
Member
 
Join Date: Jul 2007
Posts: 35
silvia is on a distinguished road
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.

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"); } }
the main:
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); } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-19-2007, 08:53 PM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
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,
Code:
public static int addAnimal(){ counter=addAnimal(); return counter++; }
What do u want from the
Code:
counter=addAnimal()
line to do supposedly?

To increment da animal counter, u don't need this line, it'll bring error toward ur work instead

Good luck with the code
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Static Variables Java Tip java.lang 0 04-17-2008 12:08 AM
Explicit static initialization with the static clause Java Tip java.lang 0 04-17-2008 12:07 AM
Error: Non-static method append(char) cannot be referenced from a static context paul Advanced Java 1 08-07-2007 06:05 AM
Error: non-static variable height cannot be referenced from a static context at line fernando AWT / Swing 1 08-01-2007 10:25 PM
non-static member can not be referenced from a static context christina New To Java 1 07-31-2007 07:11 PM


All times are GMT +3. The time now is 06:10 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org