This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes static int not incrementing, constructor not called. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static int not incrementing, constructor not called." Watch "static int not incrementing, constructor not called." New topic
Author

static int not incrementing, constructor not called.

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Hi everyone.

I'm playing around with static variables and constructors on a small program to try things out. Why won't number_cars increment and the constructors run?

CarTestDrive.java

taxi.java
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You don't have any constructors in your vehicle class. That code from lines 46 to 48 in your post is a method named vehicle(), not a constructor. Remove the return type ("void") if you want to change it into a constructor.

One more thing: although your code is syntactically correct, it would help very much if you followed the code conventions which are generally used across the Java world. Following these standards, class names are capitalized: Vehicle, Car, and so on. Variable and method names start with lower-case letters and are "camel-cased" if they consist of more than one word: numberOfCars, driveHand, and so on. Almost everybody uses these conventions, to the extent that code which doesn't follow them (like the code you posted) is hard to read.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38081
    
  22
Also you appear to have two number_cars fields. If you find our FAQ, or Google for hiding fields, you find out that proactice can be very confusing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38081
    
  22
. . . and welcome to the Ranch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static int not incrementing, constructor not called.
 
Similar Threads
Calling inner classes?
doubt in instanceof
Instance of Operator
mock test for generics & collections
DownCast and Generics