This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get the values from this  RSS feed

 
shekar march chandra
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ppl,

I am having a class called Collect, which has
1.String name. 2.int age 3. String designation attributes.

I am having
void setDetails(String nam,int a, String des) {
name= nam;
age=a;
desingnation=des;}

and
Object getDetails(){
return this; } // i am want to get all the values of the object.error here


In the main class, i instantiated Collect a = new Collect();
a.setDetails("adf",3,"fdf");
when i say a.getDetails() it is giving the object, but i
want the all the values of the object to be displayed.

Please somebody have a look at it.

Thanks,
chandra
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shekar,

Welcome to JavaRanch. We have many forums here at JavaRanch for a wide variety of topics. It becomes importatnt that the threads posting in a particular topic is about that particular topic.

In your thread here, there are no Servlets, just plain Java In General.

Well we happen to have three Java In General forums. In regard to this thread, the Java In General (Beginner) would be the most appropriate forum to post this questions. I am going to move this thread to that forum.

About your question. You now have the Object, but you need to get the instance variables. The best way is...

1) Make sure each instance variable is marked as private. For encapsulation reasons.

2) Make Getter and Setter methods for each instance variable, which returns the instance variables type. Then in your Main class call the Getters, which will return your values.

Mark
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mentioned "displayed" so I'm going to go on a bit of tangent. Say you print an object with something like this ... the first two lines are yours:

When you pass an object to println, println calls the toString() method on that object to get a printable representation of it. Your Collect object doesn't define toString() so the runtime finds the toString() method up on the base Object. If you print your getDetails() you probably get @GOBBLEDYGOOK which is the default toString() output.

You can add a toString() method to make the object display any way you like. Maybe:

Now System.out.println(a) will look more like you wanted.

Lemme know if that helps or just confuses more.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!