File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Creating Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating Objects" Watch "Creating Objects" New topic
Author

Creating Objects

Peter Pascale
Greenhorn

Joined: Jan 18, 2001
Posts: 17
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;

public static void main(String args[]) {
car accord = new car();
accord.model = "Honda";
accord.year = 1996;
test(accord.model);
// accord.vin = 18772;
infohiding(accord.vin);

truck gmc = new truck();
gmc.model = "k1500";
gmc.make = "gmc";
test(gmc.make);
truck.nextID = 425;
infohiding (truck.nextID);
}
public void start(car c) {
c.running = true;
infohiding("Car is now started");
}
public void stop(car c) {
c.running = false;
infohiding("Car is now stopped");
}
private void infohiding (String s) {
System.out.println(s);
}
private static void infohiding (long l) {
System.out.println(l);
}
private static void test(String s) {
System.out.println("test private car method + " + s);
}
protected static void protectTHIS(String s) {
System.out.println("test protected car method + " + s);
}
}
-------------------- and for Testdrive -------------------
package vehicle;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
import vehicle.*;
public class testdrive {
car porsche = new car();
porsche.year = 1999;
porsche.model = "boxster";
}
My classpath is set to ".;c:\javalib"
Both files are in c:\javalib\vehicle
When I attempt to compile testdrive.java I get:
C:\javalib\vehicle>javac testdrive.java
testdrive.java:15: <identifier> expected
porsche.year = 1999;
^
testdrive.java:16: <identifier> expected
porsche.model = "boxster";
^
testdrive.java:14: cannot resolve symbol
symbol : class car
location: class vehicle.testdrive
car porsche = new car();
^
testdrive.java:15: cannot resolve symbol
symbol : class year
location: package porsche
porsche.year = 1999;
^
testdrive.java:16: cannot resolve symbol
symbol : class model
location: package porsche
porsche.model = "boxster";
^
testdrive.java:14: cannot resolve symbol
symbol : class car
location: class vehicle.testdrive
car porsche = new car();
^
6 errors
Thanks for any suggestions.
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
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
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
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).]
bhooshan iyer
Greenhorn

Joined: Jan 13, 2001
Posts: 10
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating Objects