This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner question on objects  RSS feed

 
Elizabeth Trout
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've programmed in many different languages over the years - basically self-taught all of them except the first (which, believe it or not, was PL1) - but I am COMPETELY NEW to Java (I won't go into why I'm trying to learn Java - suffice it to say that 'just in time learning' for me is usually 'baptism by fire'!.

I've just started reading Head First Java. I'm more or less ok with the object oriented stuff except for a concept (well, a particular example) I can't seem to get my head around.

There's an example in chapter 3, page 62. Basically...

class Dog {
String name;
public static void main(String[] args) {
//make and access a Dog object
Dog dog1 = new Dog();
dog1.bark();
dog1.name = "Bart";

...... and so on including the method
public void bark()
......

OK - I understand and follow the example fine, as long as I don't try to comprehend one thing......

The code here is for a class 'Dog' - but within the code for the class - it creates an object reference to itself? Something seems circular to me, although I can't really explain it.

My brain wants to look at it as the class 'Dog' with the instance variable name and the method 'bark' and then another class 'DogTest' which would have the 'main' method and create the dog1 object.

Does my question make sense or am I thinking about this too much?

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Elizabeth!

The thing that may be eluding you is that the main() method is declared as static. That means that when it executes, there is no instance of Dog bound to the method. The method creates an instance to operate upon.

Note that this is not a common pattern for most classes. It's being used in the example because main() is the starting point for an applciation, so it's convenient to just place it in Dog.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your question makes perfect sense, and you could do it that way, in most cases, for class protocol, you should.

but in the example you showed, all the program does is create an inner
main class and create a new instance of the outer class and call some of its set/get methods.

Justin
 
Elizabeth Trout
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow - thanks for the really quick responses!

Some of this is over my head at the moment - but I do remember reading about the 'static' method and 'inner' classes as I've been skimming through the book (my 'baptism by fire' means I'm trying to fix code without knowing anything about the language yet - don't even ask why the developer isn't debugging anything). I'll go over that part again.

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin's terminology is not correct. There is no inner class in the Dog example.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Elizabeth Trout:
Wow - thanks for the really quick responses!


You think that is something, wait until you find out how many of them are Ph.D. -- I am not joking, it is not to be believed.

I recognize their work readily, if I fail you ~ one of them will be here shortly,....

I feel I grasp your question, and it is super-simple but along with some other get going questions, is not properly explained to the newbie.

you describe a class:

// in file someClass.java

public someClass {
// code goes here......

We will now write a string class.

Don't worry about all the extras and fancies,.... in that class ~ we have a constructor:

Which ideally addresses initialization issues.

Okay, answer to your question.

In an instance of the String, class we can call getChars() - a method, thus:



At this point of calling main() from outside the program, the name myString gives you a way to refer to the literal hubba bubba in ops and procedure:   .getChars()   is called a method call. This method is described in the String class. It is the way Java works, any class that has a main can be run as a standalone program. The second lowercase label firstEffort is now a variable. The issue of circular references appears to be reasonable, but in that the entire design philosophy is based on this coding technique, the issue of main being inside a class is taken into account at program load and run time.

Make sense ?

[ May 14, 2007: Message edited by: Nicholas Jordan ]

[ May 14, 2007: Message edited by: Nicholas Jordan ]
[ May 20, 2007: Message edited by: Nicholas Jordan ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas, no disrespect, but what does your reply have to do with the original question?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The practice of having a main() that creates an instance of the same class for testing or demonstration is pretty common. It's less common now than a few years ago, because most of us write tests with a testing framework of some kind but it still works just fine.

Maybe you've done trees or linked lists in other languages. A node in one of those usually has pointers or references to other nodes. They will show up as member variables of the same type as the class we're in, very much like the local variable you made in main()

There's no problem until you accidentally do this:

Can you see where that would go bad?
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
... what does your reply have to do with the original question?

Okay, I think I see your point. I edited my sample code at the end to put a main() - which I had left out.
[ May 14, 2007: Message edited by: Nicholas Jordan ]
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Man, few scholars in the Beginner's forum?? Great!!!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66204
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Jordan:

Okay, I think I see your point. I edited my sample code at the end to put a main() - which I had left out.


Still not getting it. Elizabeth asked a simple question about instantiating objects from a static method, and your reply starts talking about strings and getChars. Not seeing the connection.
[ May 14, 2007: Message edited by: Bear Bibeault ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Still not getting it. Elizabeth asked a simple question about instantiating objects from a static method, and your reply starts talking about strings and getChars.

note: (do you mean I am Not seeing the connection ?)

Poster states:
The code here is for a class 'Dog' - but within the code for the class - it creates an object reference to itself?

Well to get out of this, we have to instantiate an object - no ? To do this, I first took some sample class, gave it a method, then instantiated that class in a static method, using main() to provide a clear entry point.

[Elizabeth Trout]: Something seems circular to me, although I can't really explain it.

It's very obvious to me, entry main() inside a class, and declaration v instantiation ~ if my explaination is unclear, I stand ready to reword it.

[Elizabeth Trout]: My brain wants to look at it as the class 'Dog' with the instance variable name and the method 'bark' and then another class 'DogTest' which would have the 'main' method and create the dog1 object.

See, Ursine Master, she pulls the instantiation out of the class where originally described and places it in a second class, thus avoidiing what is intuitively an an object reference to itself.

My "... reply starts talking about strings and getChars. " was to ..... perhaps it would be better if I used posters original code. Is that what you have in mind ?
[ May 20, 2007: Message edited by: Nicholas Jordan ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!