This question already has answers here:
How to inherit static field and change it's value?
(4 answers)
Closed 7 years ago.
Here is the sample program:
public class Base {
public static final String FOO = "foo";
public static void main(String[] args) {
Base b = new Base();
Sub s = new Sub();
System.out.print(Base.FOO);
System.out.print(Sub.FOO);
System.out.print(b.FOO);
System.out.print(s.FOO);
System.out.print(((Base)s).FOO);
}
}
class Sub extends Base {
public static final String FOO="bar";
}
When I execute this it's printing foobarfoobarfoo.
Since String FOO is declared as public static final, my understanding is its value cannot be changed anymore. But in the subclass Sub, the value is being changed to bar.
Shouldn't the program throw a compilation error?
Why is it printing foobarfoobarfoo?
Static variables are not inherited, they belong to the class, that's why they are static. The subclasses can have static fields with the same, though.
Related
This question already has answers here:
What is the scope of variables declared inside a static block in java?
(4 answers)
Closed 3 years ago.
I was working on some code in which I need to access the variable "hs" present in the static block of one class from another.
Note: Both the class are preset in different packages.
Code is as follow:
public class A{
static {
HashSet<String> hs = new HashSet<>();
}
}
I Googled about it but nothing found anything helpful.
Your help would be very appreciable.
EDIT: I am not allowed to make changes in this file still need to access it from the other file.
Why I need to do this cause I am doing unit testing by JUnit and there is nothing what this block is returning which I can put assertEquals() on. So the option I left with is to test the side-effects and this variable "hs" value is getting changed as a side-effect. That's why I need to access it from another file.
Declare it as public static inside the class and initialize it in static block
class A1{
public static HashSet<String> hs;
static {
hs= new HashSet<>();
}
}
Need create getter and setter for variable "hs".
Class 1:
public class Test {
public static HashSet<String> hs;
static {
hs = new HashSet<>();
hs.add("Test14");
hs.add("Test15");
hs.add("Test16");
}
public static HashSet<String> getHs() {
return hs;
}
public static void setHs(HashSet<String> hs) {
Test.hs = hs;
}
}
Class 2
If you need to use "hs" variable in without static method then:
public class Test2 {
public void test() {
Test ts = new Test();
ts.getHs();
}
}
If you need to use "hs" variable in with static method then:
public class Test2 {
public static void test() {
Test.getHs();
}
}
This question already has answers here:
non-static class cannot be referenced from a static context [duplicate]
(6 answers)
Closed 4 years ago.
public class Test {
class Foo {
int[] arr;
Foo(int[] userInput) {
arr = userInput;
}
}
public static void main(String[] args) {
int[] userInput = new int[]{1,2,3,4,5};
Foo instance = new Foo(userInput);
}
}
It gave me an error
error: non-static variable this cannot be referenced from a static context
I already have searched some answers but cannot cannot solve it.
Here's how I think about this code, I view userInput as a pointer, and the compiler allocate five int memory and assign userInput with a address, then I transfer this address (I know java is pass by value) to class Foo constructor, and I think instance field arr got the address value.
That's how I understand, am I wrong?
Since class Foo is a non static inner class of class Test, an instance of class Foo can not exists without an instance of Test. So either change Foo to be static:
static class Foo {
int[] arr;
Foo(int[] userInput) {
arr = userInput;
}
}
or if you don't want to make it static then change the way you create an instance of Foo via an instance of Test:
Foo instance = new Test().new Foo(userInput);
When non-static nested classes (i.e. Foo) are instantiated, such as with new Foo(userInput), they need to store an implicit reference to the this variable of their enclosing class (i.e. Test). Since Foo is instantiated within the context of the static main method, no such instance of the enclosing Test is available. Thus, an error is thrown. The way to fix this is to make the nested class Foo static, as in:
public class Test {
static class Foo { // <---- Notice the static class
int[] arr;
Foo(int[] userInput) {
arr = userInput;
}
}
public static void main(String[] args) {
int[] userInput = new int[]{1,2,3,4,5};
Foo instance = new Foo(userInput);
}
}
For more information, see the Nested Classes documentation and Why do I get “non-static variable this cannot be referenced from a static context”?
This question already has answers here:
Inner classes in Java - Non static variable error
(4 answers)
Closed 5 years ago.
I come from C++ and started with Java. So I know i cant use super and this in static functions but whats wrong with the code?
class TestClass {
public static void main(String[] args) {
Test x = new Test(); // Here is the error Why?
}
class Test {
//attributes
String attribute;
}
}
Thx for your help!
Test class is an inner class of TestClass class. Therefore, in order to create an object of Test class, you must create an object of the enclosing TestClass class.
You can fix this error by moving Test class outside TestClass:
class Test {
//attributes
String attribute;
}
class TestClass {
public static void main(String[] args) {
Test x = new Test();
}
}
or by making it a nested (static) class (which doesn't require an enclosing instance):
class TestClass {
public static void main(String[] args) {
Test x = new Test();
}
static class Test {
//attributes
String attribute;
}
}
If you insist on keeping Test an inner class, you can write:
class TestClass {
public static void main(String[] args) {
TestClass.Test x = new TestClass().new Test();
}
class Test {
//attributes
String attribute;
}
}
Because, Test is non-static class and trying to access it from static context (i.e. from main() method, which is static), which means you need to create the instance/object of TestClass class first.
Test x = new Test();
It should be
TestClass.Test x = new TestClass().new Test();
Or declare Test as static.
static class Test {
//attributes
String attribute;
}
This question already has answers here:
Why can't we use 'this' keyword in a static method
(9 answers)
Closed 5 years ago.
I have a question about this fragment of code :
public class Inner {
static int a;
public static void main(String[] args) {
a = 0;
}
public static void g() {
this.a = 0;
}
}
`
Why we can't use "this.a" in static method, but we can use "a" without "this"?
Photo of compilation error: https://www.dropbox.com/s/5q6y3ldsf37p0h3/%D0%97%D0%BD%D1%96%D0%BC%D0%BE%D0%BA%20%D0%B5%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202017-05-27%2017.28.34.png?dl=0
Because this points to an instance of the class, in the static method you don't have an instance.
The this keyword refers to the current instance of the class. Static member functions do not have a this pointer
You'll notice the definition of a static member is
Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object
Which is why this has nothing to point to.
This question already has answers here:
Are static variables inherited
(4 answers)
Closed 4 years ago.
We know that in java static variable are not inherited. But in below code I am not getting any error as I want to initialize the static variable in child class.
class s
{
static int x;
}
class aaa extends s
{
void fun()
{
x=2;
System.out.println(x);
}
public static void main(String args[])
{
aaa w=new aaa();
w.fun();
}
}
static members are most definitely accessible from subclasses, as your example shows. You cannot override them, of course, but you could hide them.