Creating Objects

OK, here is what must be a simple one, but very frustrating. I'm working through Thinking in Java, with some help from another Java reference. Both make it sound real common to do what I want to do. Which is: 1-Create an object (called car) 2-Assign properties and methods to the car 3-Get car to compile and run by itselt 4-Create a car instance in another object (testdrive) Steps 1-3 are easy and work. Step for confounds me. Do I misunderstand the use of objects? Here is my code: package vehicle; /** * Title: * Description: * Copyright: Copyright (c) 2001 * Company: * @author * @version 1.0 */ import java.util.*; class truck { public String model; public String make; public static long nextID; } public class car { public String model; public String make; private int year; public boolean running; public final long vin = 1872; private String test; protected String drivername;

Peter P. You can't allocate a car object and assign the values the way you coded. There are several ways to achieve what you want.

You can also use a static initializer block to achieve the same thing without using a method body to assign the values.

BTW, you also need to change the accessor modifier to public for the year variable. -Peter

Peter Pascale
Greenhorn

Joined: Jan 18, 2001
Posts: 17

posted Jan 18, 2001 15:14:00

0

Thanks. I will try that. A follow-on question - I probably mis-understand the word Static, but how come the instance of car (porsche) that I create in testdrive has to be static. What if I had an object called 'garage' and wanted to create a number of cars. Wouldn't static limit this. SOrry, I'm a little confused about the use of static in this case. Also, I left some of the car fields private to play with scope/access, and see what happens when I try to use private fields at compile and run time. I'm expecting errors just playing and learning.

Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783

posted Jan 18, 2001 15:27:00

0

Peter P., The short answer is it doesn't have to be static. However, in my example I had to make it static because I'm accessing the variable porche from a static method (i.e. main()). Java enforces the rule that static methods can only reference static variables. The static denotes class level scope. For variables, this means that there is only one instance of the variable for all objects of the class. For static methods, there is no "this" reference to an object. This means you can invoke a static method via the class name without having to create an object for that class. Without a "this" variable, you can't refer to any non-static class instance variables. All non-static class instance variables belong to an object of that class. If you wanted to change your program to not use static, it would look something like this:

[This message has been edited by Peter Tran (edited January 18, 2001).]

peter t., err... i have a lill doubt, nothing major, perhaps even silly, can u tell me what statements like this mean? test(accord.model); test(gmc.make);

???

Peter Pascale
Greenhorn

Joined: Jan 18, 2001
Posts: 17

posted Jan 20, 2001 13:21:00

0

Sorry, bhooshan - my fault for using vague method names. In the code in the first post, you'll see that in the car class, there is a method called 'test' which prints a string. So test(accord.model) or whatever the line was, just send the model field of the accord object (which is a string) to the test method, which prints it to standard output. Peter