GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Java Programe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
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: 24187
    
  34

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: 216

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: 375
    
    1

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..
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

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
 
GeeCON Prague 2014
 
subject: Java Programe