Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Basic Constructor question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8432
606
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76486
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ahmed...

Now I get it

 
For my next trick, I'll need the help of a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic