Jump to content

Question on static variables in java, specifically the Calendar class.

I am trying to create a program where the user can create Alarms.  When i try to set a variable to the current time in the constructor, everything is fine, but when i try to do it in a method, I get the compiler message: error: non-static variable hour cannot be referenced from a static context

hour = now.get(Calendar.HOUR_OF_DAY);.  Why is this?  How can I compare the time the user inputs to the current time?

 

Here is my class:

 

import java.util.*;

 

public class Alarm

{

 

        int timeHour;

        int timeMinute;

        int date;

        Calendar now;

        int hour;

        int minute;

 

        public Alarm(int x, int y)

        {

                Calendar now = Calendar.getInstance();

                System.out.println("Alarm Initialized");

                timeHour = x;

                timeMinute = y;

                int hour = now.get(Calendar.HOUR_OF_DAY);

                System.out.println(hour);

        }

 

        public static boolean goOff()

        {

                hour = now.get(Calendar.HOUR_OF_DAY);

//              if (now.get(Calendar.HOUR_OF_DAY) == timeHour && now.get(Calendar.MINUTE)== timeMinute)

//                      return true;

//              else

//                      return false;

        }

 

}

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.

 

At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.

 

So the class says "all cars have a color" and the instance says "this specific car is red".

 

In the OO world, you define the class and inside the class, you define a field of type Color. When the class is instantiated (when you create a specific instance), memory is reserved for the color and you can give this specific instance a color. Since these attributes are specific, they are non-static.

 

Static fields and methods are shared with all instances. They are for values which are specific to the class and not a specific instance. For methods, this usually are global helper methods (like Integer.parseInt()). For fields, it's usually constants (like car types, i.e. something where you have a limited set which doesn't change often).

 

You need an instance of the class to access non-static members of that specific class.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

 

You must understand the difference between a class and an instance of that class. If you see a car on the street, you know immediately that it's a car even if you can't see which model or type. This is because you compare what you see with the class "car". The class contains which is similar to all cars. Think of it as a template or an idea.
 
At the same time, the car you see is an instance of the class "car" since it has all the properties which you expect: There is someone driving it, it has an engine, wheels.
 
So the class says "all cars have a color" and the instance says "this specific car is red".
 
In the OO world, you define the class and inside the class, you define a field of type Color. When the class is instantiated (when you create a specific instance), memory is reserved for the color and you can give this specific instance a color. Since these attributes are specific, they are non-static.
 
Static fields and methods are shared with all instances. They are for values which are specific to the class and not a specific instance. For methods, this usually are global helper methods (like Integer.parseInt()). For fields, it's usually constants (like car types, i.e. something where you have a limited set which doesn't change often).
 
You need an instance of the class to access non-static members.

 

Thanks, but I was already aware of this.  I guess I just don't know how to actually use this in real code.  I thought that static variables were only supposed to be used for things that never change (like you'd say "int RIGHT_ANGLE = 90;"), and ... well i guess I never knew what static methods would be used for.  Wouldn't you create an instance in the main method, and pass variables to the constructor?  Please elaborate, and thanks a lot for taking your time to help me.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks, but I was already aware of this.  I guess I just don't know how to actually use this in real code.  I thought that static variables were only supposed to be used for things that never change (like you'd say "int RIGHT_ANGLE = 90;"), and ... well i guess I never knew what static methods would be used for.  Wouldn't you create an instance in the main method, and pass variables to the constructor?  Please elaborate, and thanks a lot for taking your time to help me.

No, those are CONSTANTS.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

No, those are CONSTANTS.

Ok, that makes sense.  Can you point me to some code that is a good example of how to use static variables and methods?  I learn best from examples.

Link to comment
Share on other sites

Link to post
Share on other sites

@CakesCakesCakes you should remove that "static" from goOff method declaration, that will certainly solve that kind of exception.

PS: You should use "final" modifier to constant variables. Static variables are shared across all objects of one class. Static methods are accessed by the class (Alarm.staticMethod()) and can access only static variables (that's why you're getting an exception, because now is an object variable).

 

Oh my god I'm a huge idiot.  I completely didn't know that I put that final there, and didn't look at it.  I thought that the previous person was trying to say that it SHOULD be static.  Thanks a lot.

Link to comment
Share on other sites

Link to post
Share on other sites

If you have a Bike class, and have a static int for speed, changing the speed variable in any class would change it for all classes.

 

Here's an example:

Bike bike1 = new Bike();

Bike bike2 = new Bike();

 

bike1.setSpeed(100);

bike2.getSpeed(); <-- Assuming the speed variable is static, this will return 100

 

Also, static variables and methods can be accessed without initiating the class. So if you only plan on having a single instance of a class and want it to be accessible from any other class without passing along variables, you can just make everything in that class static.

Link to comment
Share on other sites

Link to post
Share on other sites

Also, static variables and methods can be accessed without initiating the class. So if you only plan on having a single instance of a class and want it to be accessible from any other class without passing along variables, you can just make everything in that class static.

 

Not wrong but it should be noted that such a design - usually called a "singleton" - is often considered to be an anti-pattern these days and to be avoided in favor of more robust and testable solutions.

Link to comment
Share on other sites

Link to post
Share on other sites

Also, static variables and methods can be accessed without initiating the class. So if you only plan on having a single instance of a class and want it to be accessible from any other class without passing along variables, you can just make everything in that class static.

 

Should also be noted in Java *cough android*, static variables do not always retain their values.  ie when you temporarily go away from the app in android the GC might eliminate the static variable values, so you have to save and recover those values.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×