Beef.
Beef.

Reputation: 41

Java newbie - cannot find symbol - variable

let's say I have : public static int MIN = 0; in a class.

and I am trying to use this constant in another class with main : myVar = MIN;

why am I getting cannot find symbol - variable MIN error? both classes are public and so is the constant MIN.

any help would be appreciated. thank you fellow coders

e; thank you guys, it worked!

Upvotes: 2

Views: 880

Answers (5)

user3465288
user3465288

Reputation:

You need to type myVar = ClassName; instead of just myVar = MIN;. you also need to import the class import ClassName.*; or import ClassName;. If you import it staticly: import static ClassName; you can do myVar = MIN;;

Upvotes: 0

mgilsn
mgilsn

Reputation: 145

You need to specify the name of class that has the attribute. For do that, follow this syntax:

ClassName.attribute;

In this case the variable is static, but if you are using a class with non-static members you first have to create a object of the class and then call it, for example:

MyClass sampleClass = new MyClass();

sampleClass.attribute;

Upvotes: 2

k88074
k88074

Reputation: 2164

Then you need to access the value with ClassName.MIN, where ClassName is the name of the class which includes MIN.

Upvotes: 2

Android Killer
Android Killer

Reputation: 18499

You should search on this simple thing and you would have got a lot of links,anyway try this, the where you defined

public static int MIN = 0;

try like this

myVar = that_class_name.MIN

Hope it will work.

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201447

In order to use a class constant like MIN from a class A, you would need to

import static A.MIN;

or you could use

int myVar = A.MIN;

Upvotes: 5

Related Questions