Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Regarding</p> <blockquote> <p>How to call a non-static method from main in Java?</p> </blockquote> <p>Short answer: You don't, at least not directly. The canonical answer is to create an instance of the class with the method, and then call the method on that instance, but in your case, it would be worthless since the class has nothing but static fields and methods. This makes your toString method meaningless in a class that is filled with static everything. Either make the method static (and it will not be a true <code>toString()</code> override) or create a true class with non-static fields and methods.</p> <hr> <p>The correct course is to get rid of all static <strong>everything</strong> from your Sphere class, the critical class here, and then give it a decent <code>public String toString()</code> method override. Of the two classes you've posted, it is the only class where a <code>toString()</code> method makes sense, but again, only if it is a true object oriented class with non-static methods and fields.</p> <hr> <p>Next, you need to give Sphere getter and setter methods that make sense. This:</p> <pre><code>public static double inputDiameter(double diameter) { return diameter; } </code></pre> <p>is neither fish nor foul. Get rid of it and create true getter and setters, the getters taking no parameters but returning a value and the setters taking a parameter and declared to return void.</p> <hr> <p>Next within the static main method, you will create a Sphere variable and assign it a new Sphere instance. Then you can call the appropriate methods of Sphere, including its <code>toString()</code> method.</p> <hr> <p>So in short, scrap all your code and start over. Start with the most important bit -- creating a decent Sphere class. Then create your Assignment class, the one with the main method, and create your Sphere instance.</p> <p>If you're still confused, post your <em>actual assignment instructions</em>, not your interpretation or paraphrasing of them, so we can see exactly what you're supposed to do.</p> <hr> <p>For example, using a related example that is similar but not the same as yours:</p> <p>CircleTest.java</p> <pre><code>public class CircleTest { public static void main(String[] args) { Circle circle = new Circle(10.0); System.out.println(circle); // calls toString() } } </code></pre> <p>Circle.java</p> <pre><code>public class Circle { private double diameter; public Circle(double diameter) { this.diameter = diameter; } public double getDiameter() { return diameter; } public double calculateArea() { return Math.PI * diameter * diameter / 4.0; } public double calculateCircumference() { return Math.PI * diameter; } @Override public String toString() { String result = String.format("Circle with diameter %.2f, area %.2f, " + "circumference %.2f", diameter, calculateArea(), calculateCircumference()); return result; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload