I'm a beginner in java and I want to have this clarification.Is passing objects in a method is more efficient than calling the method by a class instance?It looks to me the same.Suppose the following example code:
public class Test {
public static void main(String[] args) {
CircleWithPrivateDataFields myCircle = new CircleWithPrivateDataFields(5.0);
printCircle(myCircle);
}
public static void printCircle (CircleWithPrivateDataFields c) {
System.out.println("The area of the circle of radius "
+ c.getRadius() + " is " + c.getArea());
}
}
If I define CircleWithPrivateDataFields
class,this passing method to objectprintCircle
method will print on the console.Also, usually when I use void method, I need to write inside main method System.out.print(void method);
to print void method on the console but when I pass object in the method, I don't need to do that.