summer_gsr

Greenhorn
+ Follow
since Jan 12, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by summer_gsr

Ben,
I had the same doubt today and this is what I figured out, main is a static method and you cannot refer any non static methods or variables in a static method. In your example you are refering to a non static variable f1 in a static method main,so it wouldn't work. It is not the question of it being in the same class it is just that non static variables and methods cannot be refered in a static method.
Below is a small example I used to understand the concept:
class StaticTest
{
int age;
public static void main(String args[])
{
age=10;
System.out.println(age);
}
}
Here, age is a instance variable and Iam refering that in a static method main so I got a complilation error saying 'Can't make a static reference to nonstatic variable age in class Static'
But if you say int age=10; in the main and notice that they is no problem even if age is a instance variable.
Hope this helps,
Summer
Jane,
Thank you, Iam now clear why they shouldn't be declared in a method it is because static variables and methods are class members and declaring them in method is just further restricting them and so they will be a complilation problem.
Thanks,
Summer
I taught you were asking whether the result was given in a double format? Iam just a beginner in java so I don't know why but for discussion , I think the reason is that just because the variables h and f were defined with a suffix does'nt mean that the answer should be printed out as 3.0f. It just means that it is defined as a float type insead of double. Try to print the variable h the output will be 2.0 and not 2.0f.
Summer
Ajith, Iam a beginner and Iam kind of confused here. Can you please explain the actual reason why I cann't declare static variables in methods. I understand that it is a rule but why?
Thank You,
Summer
Java considers a floating point number to be a double, unless it has suffix of an 'f' or 'F' charecter. Since you used that suffix the answer is given in float type.
Summer
Hi,
Iam learning java and I confused with the concept of static modifier. I was hoping if anyone can clarify my doubt.
class StaticTest
{
static int y;
int x;
static void method()
{
static int a=10;
int b=30;
System.out.println("a "+a+"b "+b);
System.out.println("y "+y+"x "+x);
}
This is giving me a compilation error because I defined a static variable in a static method. Why cann't I declare a static variable in a static method.
Summer
Hi,
Iam learning java and I confused with the concept of static modifier. I was hoping if anyone can clarify my doubt.
class StaticTest
{
static int y;
int x;
static void method()
{
static int a=10;
int b=30;
System.out.println("a "+a+"b "+b);
System.out.println("y "+y+"x "+x);
}
This is giving me a compilation error because I defined a static variable in a static method. Why cann't I declare a static variable in a static method.
Summer
23 years ago