• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Class Function Question

JC0133

Senior member
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
}
 
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.
 
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 🙂.
 
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
 
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:
Back
Top