answersLogoWhite

0


Best Answer

if you have a function or a method that takes Object as a parameter, you can call that function or method and pass an Object as follows:

Let's say you have a class that extends Object and a function as follows

public class A extends Object {

.....

}

void function ( A arg ){

.....

}

To call this function, you need an instance of the class A

A objectInstance = new A();

function(objectInstance);

The next line shows how to pass an instance of the class A to the function.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Yes, most method calls in Java require an object to be passed as an argument.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you pass object as an argument to a method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


How many number of argument are there in round method?

The round method of the Math class is overloaded. You can either pass a double or a long into the round method


What is the use of passing an argument by value?

Passing an argument by value means that the method that receives the argument can not change the value of the argument. Passing an argument by reference means that the method that receives the argument can change the value of the incoming argument, and the argument may be changed in the orignal calling method.


What argument passes in a method that will reference the content and can change the variable in the method?

That is called passing an argument by reference.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).

Related questions

How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


How many number of argument are there in round method?

The round method of the Math class is overloaded. You can either pass a double or a long into the round method


What happens when you pass an object as an argument to addAll fucntion of arraylist?

If the passed object extends Collection, then all the objects in collection are added to the arraylist.


How do you pass an array in Java?

The same way you pass any other argument. int[] integerArray = new int[] {5,4,3,2,1}; // Pass our integerArray to the Arrays.sort method Arrays.sort(integerArray);


Differences between pass by value pass by reference in java?

pass by value is the technique where a copy of the variable is passed to the method as argument. This value can be modified inside the method but that would not affect the original value. Pass by reference is the technique where the reference to the actual variable is passed to the method as argument. Any changes to this variable would affect and alter the original value. Usually primitive data types are passed by value and objects are passed by reference in java.


What is the use of passing an argument by value?

Passing an argument by value means that the method that receives the argument can not change the value of the argument. Passing an argument by reference means that the method that receives the argument can change the value of the incoming argument, and the argument may be changed in the orignal calling method.


Explain the importance of this keyword in java?

this is treated like a pointer to the object it's written in. It's important if you want to call a method with the object calling it as an argument, or something like that.


What argument passes in a method that will reference the content and can change the variable in the method?

That is called passing an argument by reference.


What character separates each argument passed to a method?

A comma. For example, if your method name is Method1, and you wanted to pass String a, String b, and int c to it, you would do this: Method1(a, b, c);


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


How does java pass primitive types and objects?

Java uses only pass by value. Primitive data types are passed purely as pass by value whereas for objects a value which is the reference to the object is passed. Hence the whole object is not passed but its reference gets passed. All modifications to the object in the method would modify the object in the Heap.


Which method is used to modify the string stored in a StringBuffer object?

public synchronized StringBuffer append(String s)This method will update the value of the object that invoked the method, whether or not the return is assigned to a variable. This method will take many different arguments, including boolean, char, double, float, int, long, and others, but the most likely use on the exam will be a String argument-for example,StringBuffer sb = new StringBuffer("ferrari ");sb.append("car");System.out.println(sb); // output is "ferrari car"public StringBuffer delete(int start, int end)This method returns a StringBuilder object and updates the value of the StringBuilder object that invoked the method call. In both cases, a substring is removed from the original object. The starting index of the substring to be removed is defined by the first argument (which is zero-based), and the ending index of the substring to be removed is defined by the second argument (but it is one-based)! Study the following example carefully:StringBuffer sb = new StringBuffer ("0123456789");System.out.println(sb.delete(4,6));// output is "01236789"public StringBuffer insert(int offset, String s)This method returns a StringBuilder object and updates the value of the StringBuilder object that invoked the method call. In both cases, the String passed in to the second argument is inserted into the original StringBuilder starting at the offset location represented by the first argument (the offset is zero-based). Again, other types of data can be passed in through the second argument (boolean, char, double, float, int, long, and so on), but the String argument is the one you're most likely to see:StringBuffer sb = new StringBuffer ("ferrari");sb.insert(4, "---");System.out.println( sb ); // output is "ferr---ari"