Class Function Question

JC0133

Senior member
Nov 2, 2010
201
1
76
This is for Java

I am just trying to double check if this is the correct way to create a function for a class and return that type.

//this is inside of the another class or Object2
public Object1 functionName(Object1 variable1) {

//Do I even need to do this below? Cause I am passing it in as a parameter
Object1 variable = new Oject1();


//code in here

return variable1; //returning Object1 variable1
}
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,634
4,562
75
1. Is this homework?
2. You misspelled "Oject1"
3. Did you mean "variable1" on the same line? If so the answer is no. Otherwise you're creating a second instance of Object1, which likely could make sense.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I am just trying to double check if this is the correct way to create a function for a class and return that type.

You're not being terribly clear about what you want the method to do. Expand on what you mean by "create a function for a class and return that type."

Methods look like ...

[access] return-type method-name([argument-list]) { //... }

If that's too general you need to be more specific. Programming is about details, so give them :).
 

hf2046

Junior Member
Sep 23, 2011
18
0
0
This is for Java

I am just trying to double check if this is the correct way to create a function for a class and return that type.

//this is inside of the another class or Object2
public Object1 functionName(Object1 variable1) {

//Do I even need to do this below? Cause I am passing it in as a parameter
Object1 variable = new Oject1();


//code in here

return variable1; //returning Object1 variable1
}

As the other posters mentioned, it really depends on what you're trying to accomplish.

Here's food for thought, it's about method parameters and whether Java is pass by reference or pass by value...

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
 

Borealis7

Platinum Member
Oct 19, 2006
2,901
205
106
its usually a bad idea to change the parameters you use, as Objects in java are passed by reference and any change you make in the function affects the parameter object. so you don't have to return it at all!

example:

Code:
Name n = new Name("Larry");
n.displayName(); // prints out "Larry"

changeNameToJim(n); // function that gets a Name and changes its name member to Jim
n.displayName(); // prints out "Jim"
see? you've changed the object without returning it in the function.
 
Last edited: