member inner class variable access [duplicate] - java

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Why do I get "non-static variable this cannot be referenced from a static context"?
(8 answers)
Closed 4 years ago.
Can you please help me understand java8 compiler error on line k3.
public class Color {
private int hue = 10;
public class Shade {
public int hue = 20;
}
public static void main(String[] args) {
System.out.println(new Color().hue); // k1
System.out.println(new Color().new Shade().hue); //k2
System.out.println(new Shade().hue); //k3
}
}
Above code compiles if I comment out k3.
k1 outputs 10, k2 outputs 20.
With k3, compilation fails saying:
Color.java:11: error: non-static variable this cannot be referenced from a static context
System.out.println(new Shade().hue);
Main method is under static context,understood.
Where is non-static 'this' variable in this picture.
Thanks.

Related

Static Variables in Java [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
What is the difference between referencing a field by class and calling a field by object?
(5 answers)
Closed 5 years ago.
I know that static variables are part of class and not part of the Object . How can the following lines of code work without any problem
class M
{
static int i=0;
void Inc()
{
System.out.println("Global "+M.i);
System.out.println("Local "+this.i);
}
}
public class StaticTest
{
public static void main(String args[])
{
M m1=new M();
m1.i=99; //How can the m1 object access i variable of the class
m1.Inc();
}
}
The output i get is
Global 99
Local 99
How can the m1 object access i variable of the class?
It is the very same i variable in both cases.
unfortunately, java allows you to access static fields using non-static syntax.
That is all there is to this, nothing else behind this.
Yes, it is allowed for non-static members to access and update static members.
See this for more information here

non-static method cannot be referenced confusion [duplicate]

This question already has answers here:
Calling Non-Static Method In Static Method In Java [duplicate]
(14 answers)
Closed 6 years ago.
Receiving a non-static method cannot be referenced from static context. In this example I deleted all of the extra "stuff." All of the other examples I found had a lot of distractors that confused me.
This is for studying for a final and is NOT part of an assignment.
I do not understand why there is an issue here - troubles understanding static/non-static issue altogether.
In this case all I expect is for 5207 to be the output.
package testcase;
public class Testcase {
int number = 5207;
public static void main(String[] args) {
//int number = 5207;
int div;
div = divisor(number);
System.out.println(div);
}
private int divisor(int num){
return number;
}
Try to become a static method like this:
private static int divisor(int num){
return number;
}
Or instance the object of class Testcase :
Testcase tsc = new Testcase();
div = tsc.divisor(number);

Java custom method not working [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 7 years ago.
I'm a beginner and I'm starting to learn programming by doing some
exercises...
Why this simple java code gives me an error?
class HelloWorldEdited {
public int a = 5;
public int b = 2;
public static int sum() {
return a + b;
}
public static void main(String[] args) {
HelloWorldEdited obj = new HelloWorldEdited();
System.out.println(obj.sum());
}
}
I think it's because you are accessing "non static" properties (a, b) from a static method (sum), this operation is forbidden.
Try to change
public static int sum()
to
public int sum()
To understand the "static" modifier I suggest you to read:
official tutorial
The method sum() is static. In this method you can't access variables "non static".

What makes this line of code execute? [duplicate]

This question already has answers here:
What is the difference between a static and a non-static initialization code block
(9 answers)
Closed 7 years ago.
What makes this line of code execute?
The code gives an output of 11, but I was expecting
it to be 1
package methodcalling;
public class MethodCalling {
public static int cakes = 1;
public final static int UNIT = 10;
static{cakes += UNIT;} // what makes this line of code execute
public static void main(String[] args) {
System.out.println(cakes);
}
}
A static block is executed as the class gets loaded (i.e. directly after static variables are initialized). Therefore cakes += UNIT; is executed before main.

non static variable cannot be referenced from static context? [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 9 years ago.
I'm new to java and I have been exploring the different variable types. Right now I am trying to determine is printed when I add 1 to a byte variable of value 127 (the maximum value for a byte var). This is what my code looks like:
public class vars {
byte b = 127;
byte c = 1;
public static void main(String[] args) {
System.out.println(b + c);
}
}
and on my System.out.println line I get the error message that my non-static variables, b and c, cannot be referenced from a static context. How should I fix this? Is there a better way to do this project in general?
Thanks
try this is code to use the member variable inside the main function you need the class object
vars v= new vars();
public class vars {
byte b = 127;
byte c = 1;
public static void main(String[] args) {
vars v=new vars();
System.out.println((v.b + v.c));
}
}

Categories