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

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.

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);

How to access a method from the main function in java [duplicate]

This question already has answers here:
Calling Non-Static Method In Static Method In Java [duplicate]
(14 answers)
Closed 6 years ago.
Here's my code:
public class Wallgame {
public static void main(String[] args) {
new Hello();
}
public void Hello() {
System.out.println("Hello!");
}
}
This does not work, and I don't know why I can't access the Hello method. Thanks!
Hello method should be static and you call it by writing Hello(); in the main method.

Does static variable inherited? [duplicate]

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.

Why this code yields false for final modifier? [duplicate]

This question already has answers here:
Anonymous Inner classes and Final modifier [duplicate]
(3 answers)
Closed 5 years ago.
this is rather a theoretical question but maybe you know the specification that deep that will let you answer... Why this code yields false in terms of if the anonymous class is final? In practice the class can be considered final (there is no way to extend it without bytecode manipulation):
public class Modifiers
{
public static void main(final String[] args) throws ClassNotFoundException
{
new Modifiers().go();
}
public void go() throws ClassNotFoundException
{
final Runnable r = new Runnable()
{
#Override
public void run()
{
System.out.println("Inside runnable");
}
};
r.run();
System.out.println(Modifier.isFinal(getClass().getClassLoader().loadClass(Modifiers.class.getName() + "$1").getModifiers()));
}
}
Because the anonymous inner class that you are checking, Modifiers$1, is not final.
The variable r is final, but that does not mean that the class itself is final.

Use of Static in the example [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Closed 8 years ago.
I want to know the meaning of static in my file. Everytime, I come across an error and then find out that the word static is missed. Can you please explain when and where should the word static be used.
Here, for example in the code, even if I remove static from the variable, it shows error.
Is, it static class, so it has to be static variable ??
public class TestJavaServer {
static String xCordinate;
public static void main(String[] args)
{
readFromFile();
}
public static void readFromFile() throws IOException
{
xCordinate ="something";
}
A static field is the same in all instances of the class.
Do
TestJavaServer.xCordinate ="something";
and Bob's your uncle.
However, rewrite your code like this instead:
public class TestJavaServer
{
String xCordinate;
public static void main(String[] args)
{
TestJavaServer testJavaServer = new TestJavaServer();
testJavaServer.readFromFile();
}
public void readFromFile()
{
xCordinate ="something";
}
}

Categories