Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call a non-static method from main in Java?
    text
    copied!<p>I've been working on a lab for my Intro to Comp Sci course and I've run into this issue:</p> <p>As per the instructions, I must have every method that answer the specific question (Ex: Problem 5.9, method is fivePointNine() and must be called within main by another class (Assignment6.main). My problem here is that main is static and the final method I need to call for the lab is non-static, so it will not allow me to compile and run. The three errors I have are related to multiSphere(x). The specific error is "Cannot make a static reference to the non-static method 'multiSphere(double)' from the type Assignment6."</p> <pre><code>public class Assignment6 { public Sphere newSphere = new Sphere(); public Assignment6 A6 = new Assignment6(); public static int x; public static void main(String[] args) { x = 2 + (int)(Math.random() * ((10 - 2) + 1)); multiSphere(x); x = 2 + (int)(Math.random() * ((10 - 2) + 1)); multiSphere(x); x = 2 + (int)(Math.random() * ((10 - 2) + 1)); multiSphere(x); } public void multiSphere(double diameter) { Sphere.inputDiameter(diameter); Sphere.volumeCalc(diameter); Sphere.surfaceAreaCalc(diameter); toString(); } public String toString() { return "The diameter of this sphere is " + Sphere.inputDiameter(x) + ", the volume is " + Sphere.volumeCalc(x) + ", and the surface area is " + Sphere.surfaceAreaCalc(x) + ". "; } </code></pre> <p>My second class is called Sphere and looks like this:</p> <pre><code>public class Sphere { public Assignment6 newA6 = new Assignment6(); public static double volume; public static double surfaceArea; public static double inputDiameter(double diameter) { return diameter; } public static double volumeCalc(double diameter) { // Volume = (4.0/3.0)(pi)(r)^3 volume = (4.0/3.0) * (Math.PI) * Math.pow((diameter/2.0), 3); return volume; } public static double surfaceAreaCalc(double diameter) { // Surface area = (4)(pi)(r)^2 surfaceArea = (4) * (Math.PI) * (Math.pow((diameter/2.0), 2)); return surfaceArea; } } </code></pre> <p>I'm not exactly sure how to call the multiSphere(x) in the main method without running into the errors I've been getting. I feel like I'm missing something so simple. </p>
 

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