Does static variable inherited? [duplicate] - java

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.

Related

Method Calls Not Working [duplicate]

This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 4 years ago.
The method calls at the end of the main method are giving me an error saying "non-static method cannot be referenced from a static context" I'm not sure what I'm doing wrong in the method call.
public static void main(String[] args)
{
ArrayList<Candidate> voteCount = new ArrayList<Candidate>();
//add objects to voteCount
printListResults(voteCount);
totalListVotes(voteCount);
printListTable(voteCount);
}
public void printListResults(ArrayList<Candidate> election)
{
//some code
}
public int totalListVotes(ArrayList<Candidate> election)
{
//some code
}
public void printListTable(ArrayList<Candidate> election)
{
//some code
}
You simply need to declare these methods as static
public static void printListResults(ArrayList<Candidate> election) {
//some code
}
public static int totalListVotes(ArrayList<Candidate> election) {
//some code
}
public static void printListTable(ArrayList<Candidate> election) {
//some code
}
An alternative approach would be to instantiate an object of your class, as pointed out in the answer from JoschJava. Either way will work. Which approach you choose is partly a matter of taste and partly depends upon the needs of your application (which is beyond the scope of this question).
The problem is that you're trying to call a class method from a static method. You need to instantiate your class:
YourClass classObj = new YourClass();
classObj.printListResults(voteCount);

Scope of statics and key word "this" in java [duplicate]

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.

a function is required to be static in java [duplicate]

This question already has answers here:
cannot make a static reference to a non static method
(5 answers)
Closed 7 years ago.
The following block has an error. It requires the createArrayList function to be static. I cannot understand the reason. I appreciate if anyone can explains that to me in an understandable way.
import java.util.ArrayList;
public class Ceasefire {
public static void main(String[] args)
{
createArrayList();
System.exit(0);
}
public void createArrayList()
{
ArrayList<String> aL1 = new ArrayList<String>();
aL1.add("Item1");
aL1.add("Item2");
aL1.add("Item3");
System.out.println(aL1);
}
}
You cannot call a non-static (createArrayList) method from a static one (main). A static method can only call other static methods, but no instance methods.

can public static final string variables be overridien in sub-classes? [duplicate]

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.

Whats wrong with the given Java Code [duplicate]

This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 7 years ago.
Code is
public class ctorsandobjs {
private int a;
public int b;
public ctorsandobjs(String arg)
{
System.out.println("I got " + arg);
}
public void add(int a,int b)
{
System.out.println("Addition is " + String.valueOf(a+b));
}
public static void main(String args[])
{
ctorsandobjs c = new ctorsandobjs("You");
c.a = 12;
c.b = 15;
add(c.a,c.b); //compiler shows error here
}
}
I am using Eclipse Luna IDE and JDK 8 ...
can you tell me why compiler is showing error here.....
"Cannot make a static reference to a non static method add(int,int) from the type ctorsandobjs"
I am new to JAVA...
and if possible suggest a solution
add is a non-static method and so you have to invoke it from the object of a class
You have to do:
c.add(c.a, c.b);
You cannot reference non-static members (private int a; public int b) from within a static function.
The add method is not a static method, so you need to call it on an instance of the class ctorsandobjs, for example like this:
c.add(c.a,c.b);

Categories