Java Programming Tutorial - 47 - More on Static

0 William Huang · March 14, 2015
In the video, Bucky has:


public static int getMembers(){
  return members;
}


is it necessary to make the function static, can I just leave the static out? When would you need to make a function static?

Thanks in advance!

Post a Reply

Replies

Oldest  Newest  Rating
0 Branislav Lazic · March 14, 2015
You make static methods in case you don't have to override method. For so called: Utility classes. 

Something like: 

public static Connection getSQLConnection() {....} 

- it will always return same connection so we can use static method.

public static void writeToFile(String fileName) {...}

- to avoid opening of stream, closing etc.


public static void convertLocalDateToSQLDate(LocalDate date) {....}


- to avoid unnecessary boilerplate code every time when you have to convert LocalDate to SQL date.

If your code is part of some Java design pattern, you will not use static methods.
  • 1

Java / Android Development

107,321 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator