• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Programe

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 91
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank You Ernest Friedman-Hill .
 
Sijesh Alayaril
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi Nikhil Sagar ,

Next time onwards i will follow the code tags.

Thanks & Regards,
Sijesh
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic