0

For some reason when I'm trying to call methods using a main method or try changing variables declared outside the main method I get forced into having to change everything to static. This is fine in places but when it comes to needing to change values later in the code for example using a Scanner for input the main method just takes it to a whole new level trying to make me change the Scanner library etc.

This example shows what happens if I try calling a method.

This example shows what happens when I try alter the value of a variable declared outside my main method.

I have never faced an issue like this before when writing java code I've tried recreating the classes/ project files etc but nothing works. I've tried looking everywhere for a solution but I can't seem to find one probably due to the fact that I don't know what to search for. I've probably made myself look like a right idiot with my title haha! Any suggestions people?? Thanks in advance!

Maisy

maisyk
  • 21
  • 4
  • Static doesnt mean it cannot change. I suggest you to read stuff about Object Oriented Programming – Wietlol Mar 23 '17 at 18:27
  • Also worth reading: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html Honestly, just a Google search for "java static" finds *a lot* of information about what the `static` keyword means. The compiler isn't telling you to put it everywhere, nor should you. The compiler is telling you that the current combination of static and non-static members is invalid and that you need to correct it. Just making *everything everywhere* static doesn't correct it. – David Mar 23 '17 at 18:30
  • I know what static means but why on earth is it forcing me to change it to static when normally I've never had to do such a thing? – maisyk Mar 23 '17 at 18:31
  • 1
    @maisyk: `"I know what static means"` - Not according to the question you've asked you don't. `"why on earth is it forcing me to change it to static"` - It's not. That's how *you* are choosing to address the compiler errors. And that choice was incorrect. The IDE is suggesting that this *could* be a "quick fix". But the IDE doesn't know what you're trying to implement. (If it did, you wouldn't need to implement it.) – David Mar 23 '17 at 18:31
  • 2
    You have to make it static only if you are calling it statically. If you do something like `Dog d = new Dog();` then you can call `d.bark();` without using `static` on `bark`. But if you call it like `Dog.bark()` then you are calling it statically, because it's not associated with any particular `Dog` object. – D M Mar 23 '17 at 18:33
  • @DM this is off topic are you reading the book Object oriented analysis and design ?? – Mohammed Housseyn Taleb Mar 23 '17 at 18:38
  • @MohammedHousseynTaleb I'm not currently reading that book, and I really don't remember what books we used way back in college. – D M Mar 23 '17 at 18:43
  • @DM thanks that answered my question I've fixed the code and now it matches up to your Dog d = new Dog(); example and works fine now. Initially what I was doing is declaring that bit of the code outside the main method and that's what gave me the issues when trying to call methods like d.bark(); it was forcing me to change it to Dog.bark(); Thanks again :) – maisyk Mar 23 '17 at 19:00
  • DM head first is not a scholar collection but a way to become pro ... I also recommend it for @maisyk – Mohammed Housseyn Taleb Mar 23 '17 at 19:02

3 Answers3

1

When calling methods form a main you have to instantiate the class they are in, unless it's a static function

this is because that a class is a sort of template and there is nothing saved about it before it get instantiated

public class TestClass{
    public static void main(String[] args){
        TestClass testClass = new TestClass();
        testClass.method(); 
    }
    public method method(){}
}

in the example above i instantiated a TestClass and then called on the testClass instance

there is some functions and variables on classes you might want static, because a static on a class is shared between ALL instances, and can be called on the class, say you want to know how many instances were created then something like this can be done.

public class TestClass{
    public static void main(String[] args){
        TestClass testClass = new TestClass();
        testClass.method(); 
        System.out.print(TestClass.instances +""); // note that i call on
//the class and not on an instance for this static variable, and that the + "" 
//is to cast the int to a string
    }
    public static int instances = 0; // static shared variable
    public TestClass(){instances++;} // constructor
    public method method(){}
}
Mikenno
  • 294
  • 3
  • 9
1

It can be a bit confusing to get out of "static land" once you are in your main() method. One easy way is to have another object contain your "real" (non-static) top level code and then your main method creates that object and starts it off.

public static void main() {
    MyEngine engine = new MyEngine();
    // core logic inside of start()
    engine.start();
}

I hope that this was clear enough for you. Good luck Maisy!

0

You need to do some Object Oriented Programming tutorial and to learn some basic.

As answer for your problem to call without using static you have to create an instance of the main Class

let suppose the following class Foo

    public class Foo{
          private int myVarInteger;
          public int getMyVarInteger(){ return this.myVarInteger;}
          public void setMyVarInteger(int value){this.myVarInteger = value;}

          public static void main(String[] args){
                  Foo myClassInstanceObject = new Foo();
                  // now we can access the methods
                  myClassInstanceObject.setMyVarInteger(5);
                  System.out.println("value ="+myClassInstance.getMyVarInteger()); 
          }

    }