5 Replies - 752 Views - Last Post: 01 October 2013 - 02:36 PM Rate Topic: -----

#1 conure  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 99
  • Joined: 03-August 12

Simple question regarding objects

Posted 30 September 2013 - 06:46 AM

Hey all,

I have been programming for 6 months with the Sense programming language (a derivative of Scratch that my school uses to teach fundamentals). I've now started with Java but I'm slightly confused about how objects interact.

I'm following the book Head First Java, and I created two classes, one which defined a "Dog" and another which used the Dog I had defined.

I am using notepad++ and compiling via the cmd prompt, but how does my second class 'know' where to find the Dog class? I write them separately, and then compile them separately, don't designate a path or anything, yet when I follow the example it still works. Does the JVM just search for a file called Dog.class in the same folder as the folder I am located in, through the command prompt?

Sorry for such a strange question, I'm just trying to understand the fundamentals.

Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: Simple question regarding objects

#2 The Arcanist  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 37
  • Joined: 27-August 13

Re: Simple question regarding objects

Posted 30 September 2013 - 07:21 AM

It will search for the Dog.class file in the same directory where the other class is situated. If it does not find it there, it will try to find it among all the classes you have imported from other packages.

Regards
Was This Post Helpful? 0
  • +
  • -

#3 conure  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 99
  • Joined: 03-August 12

Re: Simple question regarding objects

Posted 30 September 2013 - 08:11 AM

Thank you :)
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Simple question regarding objects

Posted 30 September 2013 - 08:57 AM

As a practical matter, you can think of it as "looking in the same directory". If you do not provide a package declaration, then java will look at the local directory for your class definitions. This is fine for ad-hoc programs and your homework, but it doesn't allow a lot of control. Fortunately (or un-) it's just the tip of the iceberg. It turns out that this is actually a very productive question, and you will find that further research is very rewarding.

If you want to learn more about this, I suggest you try to find out what you can about the "classpath", packages, and the "classloader". See your favorite search engine for more detaile. The java language specification and particularly the java virtual machine specification are both good references for this*, but they are not easy reading. You will get a lot from trying to understand them, but some of the payoff will come later on, for example when you start realizing that you've already been exposed to this new concept. In the short term, you will be largely baffled, unless I miss my guess.

I don't want to blow you off by referring you to a search engine, it's just that this is a huge, huge topic and there's so much written about it that you can probably get a lot more by just grazing the literature than I could give you by writing for an hour. After you've done some grazing, I encourage you to bring back more specific questions - probably in the Advanced Discussion forum. It would be great to have some discussion of the mechanics of the jvm, and if you can bring back some good starter for that, I'd thank you for it.


*And available for free as html or pdf from oracle
Was This Post Helpful? 0
  • +
  • -

#5 conure  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 99
  • Joined: 03-August 12

Re: Simple question regarding objects

Posted 01 October 2013 - 02:19 PM

View Postjon.kiparsky, on 30 September 2013 - 08:57 AM, said:

As a practical matter, you can think of it as "looking in the same directory". If you do not provide a package declaration, then java will look at the local directory for your class definitions. This is fine for ad-hoc programs and your homework, but it doesn't allow a lot of control. Fortunately (or un-) it's just the tip of the iceberg. It turns out that this is actually a very productive question, and you will find that further research is very rewarding.

If you want to learn more about this, I suggest you try to find out what you can about the "classpath", packages, and the "classloader". See your favorite search engine for more detaile. The java language specification and particularly the java virtual machine specification are both good references for this*, but they are not easy reading. You will get a lot from trying to understand them, but some of the payoff will come later on, for example when you start realizing that you've already been exposed to this new concept. In the short term, you will be largely baffled, unless I miss my guess.

I don't want to blow you off by referring you to a search engine, it's just that this is a huge, huge topic and there's so much written about it that you can probably get a lot more by just grazing the literature than I could give you by writing for an hour. After you've done some grazing, I encourage you to bring back more specific questions - probably in the Advanced Discussion forum. It would be great to have some discussion of the mechanics of the jvm, and if you can bring back some good starter for that, I'd thank you for it.


*And available for free as html or pdf from oracle


Hi Jon,

Thank you very much for the long and detailed reply - I will look into this more in the future definitely. At the moment I struggling to even remember basic syntax! I definitely didn't feel that you'd blown off the answer at all. I think knowing it does search the directory is enough for to satisfy me for now :)

Thank you again.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Simple question regarding objects

Posted 01 October 2013 - 02:36 PM

Quote

I think knowing it does search the directory is enough for to satisfy me for now


Yes, that will work for the time being. The next thing you'll want to know about is packages, when your programs get a bit more complicated. Package declarations will change the rules on you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1