Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Programe

 
Sijesh Alayaril
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sijesh Alayaril
Ranch Hand
Posts: 37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thank You Ernest Friedman-Hill .
 
Sijesh Alayaril
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Nikhil Sagar ,

Next time onwards i will follow the code tags.

Thanks & Regards,
Sijesh
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic