0

So how I am understanding static and non static calling:

If you are calling methods, passing values and returning values without creating a new object of the Class, use static.

If you are calling methods, passing values and returning values after creating a new object of the class, don't use static.

I created two snippets of code. One is commented out but you will see in the class AreaCalculator, that the static reference coincides with the commented out methods being called in the main file.

My question on top of this in what instance would it be beneficial to be calling static or non static? If I created a bunch of non static types and primitives, then I would be able to utilize them under new objects without affecting the original value of those initializations in the class right?

The opposite would be if they were static variables, the values would be modified in the class(Since no object exists) when manipulating the data when work is performed setting them?

Code Below:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    //-----------------------------------------
    //Runs a For loop to get input, store in array, display results
    //ForLoopInputArray.looping();
    //-----------------------------------------


    //-----------------------------------------

    //Learning to return values when passing parameters
    // Area/Perimeter Calculator Methods

    Scanner apinput = new Scanner (System.in);
    AreaCalculator areatest = new AreaCalculator();

    //Using a static method
    //System.out.println("Enter a number to calculate the area: ");
    //System.out.println(AreaCalculator.area(apinput.nextDouble()));
    //System.out.println("Enter a number to calculate the Perimeter: ");
    //System.out.println(AreaCalculator.perimeter(apinput.nextDouble()));

    //using an object to perform the same task
    System.out.println("Enter a number to calculate the area: ");
    System.out.println(areatest.area(apinput.nextDouble()));
    System.out.println("Enter a number to calculate the Perimeter: ");
    System.out.println(areatest.perimeter(apinput.nextDouble()));

    //-----------------------------------------


}

}


public class AreaCalculator {

 /*static*/ double area (double areainput) {
    return Math.PI * (Math.pow(areainput, 2));

}

 /*static*/ double perimeter (double perimeterinput) {
    return Math.PI * (perimeterinput * 2);
} 

}

  • If you're making a calculator that's just a bunch of methods and constants, you should probably use static methods. If you need to store state in a bunch of fields, for example, if you have a Person class with a name field, etc., then you should have non static fields and non static methods to do stuff with those fields. – user May 21 '20 at 15:48
  • It looks like a question on when to use static keyword. Refer to this post https://stackoverflow.com/questions/2671496/java-when-to-use-static-methods. – leetcode269 May 21 '20 at 15:50
  • I think it's better if you store any mutable state outside of your calculator. For example, you can keep brand names of paint as an immutable object inside calculator and have a static method that returns the double array based on given parameters. It makes your API more concise and less prone to bugs since a computation by the calculator should always return the same value if given parameters are the same. – leetcode269 May 21 '20 at 16:14
  • I understand your description of that perfectly. I was already in the process of doing this to keep the data easier to read for myself. It makes sense. Sometimes I figure things out a little too late or while I'm in the process of doing it. – Lucas Hoage May 21 '20 at 16:17

1 Answers1

1

Non static methods are the ones that you call over an instance, for example, when you create a new AreaCalculator(). Static methods are the ones that you can invoke without having any reference of AreaCalculator(). This could help you a little bit more: What is the difference between a static method and a non-static method?

Josep
  • 162
  • 1
  • 9
  • Yeah, I'm definitely seeing that relation to creating an object and it not requiring static. I just didn't understand where it could be useful. I think the other contributors and yourself may have cleared this up fr me. I'm going to mark it as answered and follow up on these articles. It was less of a question of what is the difference and more of a question of where does it apply in practical programming. I appreciate everyone's input. – Lucas Hoage May 21 '20 at 16:01