aspose file tools*
The moose likes Beginning Java and the fly likes Returning values from a one method in a class....to another method in a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returning values from a one method in a class....to another method in a class?" Watch "Returning values from a one method in a class....to another method in a class?" New topic
Author

Returning values from a one method in a class....to another method in a class?

Gus Parker
Greenhorn

Joined: May 10, 2009
Posts: 19
Hello.
I am having huge problems, I just don't seem to be able to do it, I have tried everything, and I'm sure I'm doing something wrong. Please if anybody could help me it would be great.

I'm trying to pass a string, into a method within' another class. I know this might seem silly, but I've tried many different things, if someone could help me out or point me in the right direction?

Below is a copy of the Class's I'm working with......
I have put the problem area's (x2) and explanations in green.










Any help would be much appreciated?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

I know this might seem silly, but I've tried many different things, if someone could help me out or point me in the right direction?


Well, just what "many different things" have you tried?



The target methods doesn't take any parameterts, or return anything.



And at the source, you don't even call the target method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Write down a pair of tiny little classes which pass and return information to each other. Solve a little problem, then apply the techniques to a larger problem.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
What Campbell said !!!

Oh, that advice is SO wonderful!!!

I would guess that we were able to test 95% of the topics in our SCJP book with less than 20 lines of code.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Gus Parker
Greenhorn

Joined: May 10, 2009
Posts: 19
Ok. So i've done that and think i can apply this to my main program, before i go any further, may I ask 3 questions(below code)?










I still have a couple of questions? Maybe Im still doing it wrong but...
1. What is the point in "round.setRadius("");"...Does that sort of set it to default or something?
2. in the code chunk below, what is the point in creating "String P" ....I could only get it to work with a random string dropped in there?

public void setRadius(String p) {
radius = "Test";
}

3. Would it be wise to put these values into a constructor as well or wouldnt it matter - It does the same thing?
priya rishi
Ranch Hand

Joined: Oct 26, 2008
Posts: 119
1. What is the point in "round.setRadius("");"...Does that sort of set it to default or something?


try this,
public void setRadius(String p) {
// radius = "Test";
}

2. in the code chunk below, what is the point in creating "String P" ....I could only get it to work with a random string dropped in there?
public void setRadius(String p) {
radius = "Test";
}
This is one of the Object Oriented Concepts - Data Encapsulation , where you make the instance variables private and use the getter and setter methods(Java Beans Standards) to access them.

In your example you can set the value for radius, like this

public void setRadius(String p)
{
radius = p;
}

3. Would it be wise to put these values into a constructor as well or wouldnt it matter - It does the same thing?


constructor will be invoked only when instantiation of an object is done.
one might even use inheritance to access the members of a class.


note: the default value for String is null.

SCJP 5 , SCWCD 5
Gus Parker
Greenhorn

Joined: May 10, 2009
Posts: 19
priya rishi wrote:
1. What is the point in "round.setRadius("");"...Does that sort of set it to default or something?


try this,
public void setRadius(String p) {
// radius = "Test";
}

2. in the code chunk below, what is the point in creating "String P" ....I could only get it to work with a random string dropped in there?
public void setRadius(String p) {
radius = "Test";
}
This is one of the Object Oriented Concepts - Data Encapsulation , where you make the instance variables private and use the getter and setter methods(Java Beans Standards) to access them.

In your example you can set the value for radius, like this

public void setRadius(String p)
{
radius = p;
}

3. Would it be wise to put these values into a constructor as well or wouldnt it matter - It does the same thing?


constructor will be invoked only when instantiation of an object is done.
one might even use inheritance to access the members of a class.


note: the default value for String is null.


thanks very much for your reply priya, very helpful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Returning values from a one method in a class....to another method in a class?
 
Similar Threads
Student schedule
Good programming habits - evaluate this very short piece of code
org.hibernate.PropertyAccessException
How to detect memory leak ?
[Resolved]java.util.NoSuchElementException problem after displaying the information from text file