Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Java Object Question(Why does object process occur)
Java Object Question [message #892263] Wed, 27 June 2012 10:16 Go to next message
Alan Farroll is currently offline Alan Farroll
Messages: 50
Registered: June 2012
Member
Hello,

I am fairly new to both Java and Eclipse and just completed a tutorial named "Eclipse and Java for Total Beginners". I am also learning more about Object Oriented programming. I am confused about one process in the tutorial and wondered if someone could tell me how this occurs.

I don't want to publish all the code so in brief, I have a class of Books and a MyLibrary class.

In BOTH the Book class and the MyLibrary class the tutorial has us create a toString method to override the Java toString method, created as;

public String toString(){
// method expressions
}

In the MyLibrary class I have an ArrayList of Books and a method to print out the status of each book using the expressions

for (Book thisBook : this.getBooks()){
System.out.println(thisBook);
}

At the line, "System.out.println(thisBook) " above the program jumps to the toString method in the Book class and processes the expressions. Why does this occur? And why does it go to the toString method in the Book class and NOT the toString method in the MyLibrary class?

Thanks in advance

Alan
Re: Java Object Question [message #892268 is a reply to message #892263] Wed, 27 June 2012 10:32 Go to previous messageGo to next message
Russell Bateman is currently offline Russell Bateman
Messages: 3052
Registered: July 2009
Location: Provo, Utah, USA
Senior Member

On 6/27/2012 8:17 AM, Alan Farroll wrote:
> Hello,
>
> I am fairly new to both Java and Eclipse and just completed a tutorial
> named "Eclipse and Java for Total Beginners". I am also learning more
> about Object Oriented programming. I am confused about one process in
> the tutorial and wondered if someone could tell me how this occurs.
>
> I don't want to publish all the code so in brief, I have a class of
> Books and a MyLibrary class.
>
> In BOTH the Book class and the MyLibrary class the tutorial has us
> create a toString method to override the Java toString method, created as;
>
> public String toString(){
> // method expressions
> }
>
> In the MyLibrary class I have an ArrayList of Books and a method to
> print out the status of each book using the expressions
>
> for (Book thisBook : this.getBooks()){
> System.out.println(thisBook);
> }
>
> At the line, "System.out.println(thisBook) " above the program jumps to
> the toString method in the Book class and processes the expressions.
> Why does this occur? And why does it go to the toString method in the
> Book class and NOT the toString method in the MyLibrary class?
>
> Thanks in advance
>
> Alan

You're asking to println() an object of class Book; does it not make
perfect sense that Book's toString() method be used to express a book in
a String way for output?

Java questions are answered in forums like javaranch.com,
stackoverflow.com, jguru.com, etc. Eclipse forums answer questions on
setting up and using Eclipse.

If you're just getting started, but you're committed, I'd suggest the
following books (I'm not paid to say this, it's just my opinion):

- Java Head First (use as a fun method to learn)
- Thinking in Java (use as a grammar to look things up in)

Best of luck.
Re: Java Object Question [message #892278 is a reply to message #892268] Wed, 27 June 2012 11:07 Go to previous message
Alan Farroll is currently offline Alan Farroll
Messages: 50
Registered: June 2012
Member
Hi Russell,

Thanks for your response. Yes, this helps. I kind of thought that was what was happening but wanted some clarification. I have moved to a visual development environment role from a Unix role although I did Windows visual stuff years ago. Now just getting to grips with Java and Eclipse as I have been moved into role to assist with a project.

Thanks for links and books also.

Regards

Alan
Previous Topic:Lost my workspace after an update
Next Topic:Standalone infocenter content display on iPad
Goto Forum:
  


Current Time: Mon Apr 21 05:51:37 EDT 2014

Powered by FUDForum. Page generated in 0.02370 seconds