• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Constructor question  RSS feed

 
Jim Brent
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am working on Head First Java and am revisiting a basic concept because I just realized that I don't fully understand it.

In reference to the The 3 Steps of Object Declaration, Creation and Assignment found on page on 55 of the book, is the object type (in this case Dog) already defined in the program?

As in:



Dog myDog = new Dog();

Dog is the type of the variable (instead of a string, or an int etc...)

myDog is the name of the reference variable that is stored on the stack.

= the equal sign is the assignment operator that links the reference (myDog) to the Object (the Dog Object) on the Heap.

new Dog(); is the command to create a new Dog object on the heap ((from the Dog() class)).

Now when I type this in Eclipse I get the error which states: Dog cannot be resolved to a type.

So I need a Dog Class created first which defines the type for what??? --- The Program, the compiler, the package, or the JVM?


when I type just the word Dog I received the following error.

- Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration

So how does the IDE know that I am trying to declare a Variable reference? AND does that mean the compiler will figure this out before the JVM?

Any thoughts would be greatly appreciated.

Thank you.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Dog" in this case is a Class. A Class defines a new type.

You mentioned String and int in passing, and I would just say that String is a class, like Dog would be, but int is a primitive, not a class.
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The IDE guesses what you are trying to do based on where the text is written.

Dog isn't a class that is part of Java so you have to either create it yourself or import it.
 
Liutauras Vilda
Marshal
Posts: 4668
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, check your main method signature, and try to find, what is wrong with it? That could be difficult to notice.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim, and welcome to JavaRanch.

First: Don't check 'Disable BB Code...' - especially if you're posting code. They're not something naughty; just the "tags" that the site normally recognises.

Now:
Jim Brent wrote:Now when I type this in Eclipse I get the error which states: Dog cannot be resolved to a type.
So I need a Dog Class created first which defines the type for what??? --- The Program, the compiler, the package, or the JVM?
The compiler.

when I type just the word Dog...
Not quite sure what you mean here. You tried to create a source file with just the word 'Dog' in it?

I received the following error.
- Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration
So how does the IDE know that I am trying to declare a Variable reference? AND does that mean the compiler will figure this out before the JVM?
Yes (in general). The messages you see in an IDE usually come from the compiler; but many IDEs have their own "smarts" built in, so you can't always be absolutely sure.

To make absolutely sure, shut down your IDE and run javac from the command line.

Winston
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly NetBeans uses the default javac tool but Eclipse has its own compiler (which produces better error messages).
 
Jim Brent
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips on posting, I didn't see the Deisable BB Code...

public static void main(String[] args) {
// public static void Main(String [] args){ Thanks - I see the error now ;-)

Thanks Knute Snortum... I forgot that String is a class not a primitive!

Not quite sure what you mean here. You tried to create a source file with just the word 'Dog' in it?
--- I was just curious about how the IDE / Compiler / JVM works so I was experimenting...

Thank you everyone this is very helpful... These answer bring to mind another question...

Let's say I correctly finished these classes and compiled them, how would the JVM know that Dog is type that was defined by a class that I created?

If it were in a .jar file, would it be listed in the manifest? -Or if this was a command line program how does the JVM know where to look to find the Dog class?

Thank you for all your help.

Jim

 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be found on the CLASSPATH.

If it is in a jar, then you don't need to have it listed in the manifest of that jar, you just need that jar on the CLASSPATH. If another jar uses it, then you could have the jar which contains Dog referenced in the manifest of that other jar.

e.g.
You package Dog in Jar1.jar
In Jar2.jar you have code that needs to use Dog
Then in Jar2.jar, the manifest will contain:
Class-Path: Jar1.jar
 
Jim Brent
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ahmed...

Now I get it

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!