0

I've been running through a few tutorials for Java, they all say to make a new variable when calling classes. Why is this? I've tested some code and it works without doing this. I've been using python for quite a while now so I'm used to using a dynamic language.

Please see some code I've been playing around with below:

import java.util.Scanner; 

class MyClass {

    static String myName(String name) {
        return ("Your name is: "+name);
    }
    static String myAge(Integer age){
        return ("Your age is: "+age);
    }
    static String myGender(String gender){
        return ("You are: "+gender);
    }
}

class Test{
    public static void main(String [ ] args){
        Scanner ui = new Scanner(System.in);

        MyClass user = new MyClass();

        //Output with new variable of class - user
        String input = ui.next();
        String newname = user.myName(input);
        System.out.println(newname);

        //Output calling class directly
        Integer input1 = ui.nextInt();
        String newage = MyClass.myAge(input1);
        System.out.println(newage);

        //Output with new variable of class - user
        String input2 = ui.next();
        String newgender = MyClass.myGender(input2);
        System.out.println(newgender);

    }

}

Thanks for your time.

TJames
  • 105
  • 1
  • 1
  • 10
  • "_make a new variable when calling classes_" What do you mean by this? `MyClass` doesn't have any variables, only a few static methods. – takendarkk Mar 18 '18 at 00:34
  • 2
    Python has objects. It sounds like you just haven't used them. (Not that there's *necessarily* anything wrong with not knowing...I took a C++ class at a community college once and they never told us C++ had objects...) – Radiodef Mar 18 '18 at 00:36
  • I made a mistake by saying OOP , I meant to say I am used to using a dynamic language like python. My query is why do you make a new instance of a class like in the example: MyClass user = new MyClass(); But I am still new to programming so I apologise for any incorrect terms. – TJames Mar 18 '18 at 01:13

2 Answers2

4

If everything in the class is static (as in the code you posted), then there's no need to create instances of the class. However, if the class were to have instance fields and/or methods, then the story is different. For instance, consider a class like this:

class Person {
    private String name;
    private int age;
    private String gender;

    public Person(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    String myName() { return "Your name is: " + name; }
    String myAge() { return "Your age is: " + age; }
    String myGender() { return "You are: " + gender; }
}

Then you could create several Person instances with different internal state and use them interchangeably in your code:

public static void main(String[] args) {
    Person jim = new Person("Jim", 40, "male");
    Person sally = new Person("Sally", 12, "female");

    report(jim);
    report(sally);
}

private static report(Person person) {
    System.out.println(person.myName());
    System.out.println(person.myAge());
    System.out.println(person.myGender());
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

If we create any member with static keyword it get memory at once to all objects, static keyword we used when we have common properties in class and we don't want to create separate memory to all instances objects ... it doesn't need to create instance variable to call it and this static block is shareable to to all objects.... for example if we have Animal class and we want to describe 5 different type of dog's ... than we don't define color, size like properties as static ... because they all have their own different size and color.... I hope you get it

Bean
  • 67
  • 7
Zain
  • 452
  • 5
  • 14
  • 3
    I don't see how this helps. The grammar, spelling, punctuation are all broken. You don't address the OP's example. And by focusing on `static`, and talking about "memory" your explanation misses the real point of OO. Look at Ted Hopp's answer for an example of a good / helpful answer. – Stephen C Mar 18 '18 at 00:56
  • okk bro, i am here for learning, thanks. – Zain Mar 18 '18 at 01:02
  • 2
    That's why I commented .... – Stephen C Mar 18 '18 at 01:11