1

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 4
    There is no applicable difference - worrying about such will just lead to poorly designed code. While the Java bytecode (and work done) needed to call a virtual method and a static method are slightly different, this should probably be approached as an implementation/academic question. – user2864740 Mar 19 '16 at 22:30
  • 2
    Your question is close to meaningless, since the term "efficient" does not really apply here. I would worry more about creating classes that make sense, that have single responsibility, that have low coupling and high cohesion. – Hovercraft Full Of Eels Mar 19 '16 at 22:32
  • I understand what you are saying but if I don't clarify such concerns, chances are I will never learn.As a beginner and an aspiring student, I think it's incumbent on me to learn, even if it sounds ridiculous to the adept programmers like you.One day i will be like you too ;) –  Mar 19 '16 at 22:34
  • @Zakir For some technical details (which are not required to write good code), see http://stackoverflow.com/q/1504633/2864740 and http://zeroturnaround.com/rebellabs/java-bytecode-fundamentals-using-objects-and-calling-methods/ etc. (And this is without even considering the JIT.) – user2864740 Mar 19 '16 at 22:35
  • Why not simply give your CircleWithPrivateDataFields a decent `public String toString()` method override, and your problem's solved? Then you can simply pass an object of this class into a println method call and be good. – Hovercraft Full Of Eels Mar 19 '16 at 22:35
  • @ Hovercraft Just learned a new technique from you.Thanks and will remember. –  Mar 19 '16 at 22:37
  • @user2864740 thanks for sharing the link, I will learn. –  Mar 19 '16 at 22:38

1 Answers1

0

Cleanest is to simply give the class a toString() method override, and then you can simply print it out since the out(...) method of the PrintStream class will call the toString() method of any object passed into it:

public class CircleWithPrivateDataFields {
    private double radius;

    public CircleWithPrivateDataFields(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    @Override
    public String toString() {
        return String.format("The area of the circle of radius %.2f is %.2f", radius, getArea());
    }

    public static void main(String[] args) {
        CircleWithPrivateDataFields circle = new CircleWithPrivateDataFields(5);
        System.out.println(circle);
    }

}

Note that efficiency has nothing to do with this.

Also note that toString() methods are great as debugging help and as output for toy programs as shown above, but are almost never used as output methods for production code.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373