This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java Programe Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Java Programe" Watch "Java Programe" New topic
Author

Java Programe

Sijesh Alayaril
Ranch Hand

Joined: Jan 13, 2012
Posts: 37


Hi All,

Bellow i'm writing one small programe,


public class TrickyOne {
TrickyOne to = new TrickyOne("welcome");

TrickyOne(String a){
this.localvariable = a;
}

private String localvariable;

/**
* @return the localvariable
*/
public String getLocalvariable() {
return localvariable;
}

/**
* @param localvariable the localvariable to set
*/
public void setLocalvariable(String localvariable) {
this.localvariable = localvariable;
}

public static void main(String args[]) {
TrickyOne to = new TrickyOne("welcome");
System.out.println(to);
}
}

When your displaying 'TrickyOne object(to) ,Is it possible to display welcome.Should not do any chnages inside main method.


Thanks & Regards,
Sijesh
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

When you print an object using println(), the object's toString() method is called. So give this class a toString() method which returns what you want printed.

I should point out that the variable you've named "localvariable" is not a local variable -- it's a member variable. A local variable is one defined inside a method.


[Jess in Action][AskingGoodQuestions]
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 214

Dear Sijesh Alayaril
It is my suggestion to use code tags every time when you post a code so that we can read it without any difficulties.


OCPJP 6 86%
Sijesh Alayaril
Ranch Hand

Joined: Jan 13, 2012
Posts: 37


Thank You Ernest Friedman-Hill .
Sijesh Alayaril
Ranch Hand

Joined: Jan 13, 2012
Posts: 37


Hi Nikhil Sagar ,

Next time onwards i will follow the code tags.

Thanks & Regards,
Sijesh
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 276

Sijesh Alayaril wrote:

Also, your code can throw StackOverflowError.. Check your instance reference variable... it will be initialized for every instance of TrickyOne you create.. Which in turn again create an instance of TrickyOne..

It is a chain of instances like: - TrickyOne contains(TrickyOne contains(TrickyOne(contains TrickyOne(..... so on..))))
Beware of these kinds of designs..


OCPJP
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 895

R. Jain wrote:
Sijesh Alayaril wrote:

Also, your code can throw StackOverflowError.. Check your instance reference variable... it will be initialized for every instance of TrickyOne you create.. Which in turn again create an instance of TrickyOne..

It is a chain of instances like: - TrickyOne contains(TrickyOne contains(TrickyOne(contains TrickyOne(..... so on..))))
Beware of these kinds of designs..


there is no CAN. it will definitely throw StackOverflowError.

the other day i realised that if you are reading JLS the words CAN , MAY sometimes makes a huge difference. so i thought to correct you although i know you intended that it WILL throw error for sure. Regards


OCPJP 6(100 %) OCEWCD 6(91 %)
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 276

gurpeet singh wrote:so i thought to correct you although i know you intended that it WILL throw error for sure. Regards

Oh Yeah.. Words do matter.
Correction appreciated.. and Accepted
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Java Programe
 
Similar Threads
same variable name for instance and local variable
Clarification on local variable scope.
help me on output of this code
loop
Variable with Same Name