wood burning stoves 2.0
The moose likes Java in General and the fly likes Using static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Java in General
Bookmark "Using static methods" Watch "Using static methods" New topic

Using static methods

karim qazi

Joined: Mar 22, 2001
Posts: 20
I want to know when it is best to use static methods over non-static ones.
I know that static methods are class methods and every class that instatiates the class will get the same method.
In other words it will not have to create a new copy of that method.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

Doesn't matter if a method is static or not, all objects of that
class will have the same method.
The different between static method and non-static method, is static method can be called WITHOUT an instance of that class (aka an object) exist. Because of this static method can only
manipulate a static variable of the class.
So most of the time, you only use static method when you are doing operation that is not affecting specific instance in particular or only affecting static variables. And you use non-static method to do operation that are specific to a certain
object only.
I hope that helps.
karim qazi

Joined: Mar 22, 2001
Posts: 20
Yes that helps.
So when should I use a static constructor?
For instance if you want to initalize ceartin static variables without instantiating a object.
Also when to use static variables?
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

You use static variables when you want a variables that is common across the class or instance of the objects, it's like
a global variable.
Some example of static variables are:
where Math and Color are the class and pi, white and red are
the static variables.
For examples you want to create a class of Student, and
you want to differentiate between undergrad or graduate student.
You can do:

where when you create a Student object you can do:
Student me = new Student( Student.GRADUATE );
For initializing static variables you can do it in 2 ways:
First one, just initialize it right away:

or you can put them in static block, like this:

Hope this help.
karim qazi

Joined: Mar 22, 2001
Posts: 20
would the code below be a correct implementation of using static variables,constructors, and methods?
public class CreateDateTime{
private static GregorianCalendar todaydate;

todaydate = new GregorianCalendar();

public static String UniqueRandomNum(){
//Create UniqueRandomNum
return createDate() + createTime();

public static String createDate(){
String year =Integer.toString(todaydate.get(Calendar.YEAR));
String month = Integer.toString(todaydate.get(Calendar.MONTH) + 1);
String day = Integer.toString(todaydate.get(Calendar.DATE));

//check if month has length of 1 and attach 0
if (month.length() == 1){
month = "0" + month;

//check if day has length of 1 and attach 0
if (day.length() == 1){
day = "0" + day;
return year + month + day;

public static String createTime(){
//Create current time
String hr = Integer.toString(todaydate.get(Calendar.HOUR_OF_DAY));
String min = Integer.toString(todaydate.get(Calendar.MINUTE));
String sec = Integer.toString(todaydate.get(Calendar.SECOND));
String milsec = Integer.toString(todaydate.get(Calendar.MILLISECOND));

if(min.length() < 2) min = "0" + min;
if(sec.length() < 2) sec = "0" + sec;
if(milsec.length() < 3) milsec = milsec + "0";

return hr + min + sec + milsec;
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The problem with making today's date a static is that once the class is loaded into the VM it won't reload the static variables unless the class is dropped from memory for some reason. So it may be there tomorrow and next week without changing the date!

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link: http://aspose.com/file-tools
subject: Using static methods