0

I got into programming a bit obliquely with Bukkit and thus didn't learn some things properly. But since I've been doing real stuff for a while now I wanted to ask how to deal with static.

I know that you should avoid static as most as possible.

Should you then call external functions like this?

//Another Class
public void exampleMethodInAnotherClass() {
  system.out.prinln("Hi :D");
}

//Main
public static void main(String[] args) {
  new AnotherClass().exampleMethodInAnotherClass();
}

//OR

public static void exampleMethodInAnotherClass() {
  system.out.println("Hi :D");
}

public static void main(String[] args) {
  AnotherClass.exampleMethodInAnotherClass();
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Givou
  • 17
  • 1
  • 3
    *"I know that you should avoid static as most as possible."* that doesn't mean *"don't use static method when they make sense".* The code you're showing is a bit too simplified to give a meaningful answer, but I'd say the second way wins in this instance. – Federico klez Culloca Apr 08 '21 at 08:21
  • Not really a duplicate, but take a look at [this other question](https://stackoverflow.com/questions/2671496/when-to-use-static-methods) (and its answers, of course :) ) – Federico klez Culloca Apr 08 '21 at 09:41

2 Answers2

0

Now it's about the type of function that you use if the function is too much used in your code like System.out.println then make it static *(static function are mostly common in maths and helper classes).

Yeshwin Verma
  • 282
  • 3
  • 16
0

OH the static keyword , My most hero in the programming!

I know that you should avoid static as most as possible.

no that's not true in the most cases the programmer that are student or new in the programming think it's best Idea that we have use static key but it's important to know the why we use static. after you use static key the variable imidediately going to memory and you can accsess it directly by calling the refrence! and it's the package and class with the variable name but the static method is in the memory and if you change it from some where in your code the data change , see some example :

public class Test {
static String MESSAGE= "";

public static setMessage(String message){
MESSAGE = message;
}

public static void showMessage(){
System.out.println(MESSAGE);
}
}
----------------
Calling from another class
public static void showMessage(){
  System.out.println(Test.MESSAGE);
}

if you run the program and change the message with the showMessage method you can get the message and if you need you can call the MESSAGE by reference Like ClassName.MESSAGE or create object from your class with new Keyword but your MESSAGE variable is static and in your memory after running your code so the use new keyword to call it not nesssasery and you can call it directly ! remember using the static variable in mini or script cases or test is good idea but if you create Enterprise project using static method or variable without knowledge about it it's bad idea! because , I most use static keyword for method or variable I need always return same result or work straight work I need! like show the time , convert date or etc... but don't use for changing the data The example I share it's good ref for know the problem.

Good ref for know the static internal work it's here

  • So, for things like checkFiles() (in f.E Config classes) i shouldnt use static, and in classes i need often like createProcess or something i should....or in MySQL...for connect Method i use not static, so i can set host, password usw to private. But in getResult i use static. right? – Givou Apr 08 '21 at 09:22
  • you can use create connection as static method to create connection but you can't make connection as static . – FarshiD Naqizadeh Apr 08 '21 at 09:47
  • you can create method like : checkFileExist() or like that but in this cases you need check sync your method for threat . – FarshiD Naqizadeh Apr 08 '21 at 09:48