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.
Related
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);
This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
compilation error: identifier expected
(4 answers)
Closed 5 years ago.
Why is this error coming? Please see the following code.
class Test{
Hello h=new Hello();
}
class Hello{
int a=10;
System.out.println(a); // error identifier expected
}
Create Class With Same Package
public class Hello {
public void print(){
int a = 10;
System.out.println("Number is :" +a);
}
}
Crate Class to Set the Main Method Within the Same Package of Hello Method
public class Main {
public static void main(String args[]){
Hello h1 = new Hello();
h1.print();
}
}
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.
This question already has answers here:
How can you run a Java program without main method? [duplicate]
(4 answers)
Closed 7 years ago.
here is code
public class Test {
public static void main(String[] args){
System.out.println("Hello");
}
}
And my question is that how to print Hello World without touching main method?
I tried using static block but then Output comes like
World
Hello
But i want Hello World in same line.
Try this
public class Test {
static {
System.out.println("Hello world");
System.out.close();
}
public static void main(String[] args){
System.out.println("Hello");
}
}
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.