This question already has answers here:
Why I am not getting NullPointerException? [duplicate]
(6 answers)
Closed 6 years ago.
public class Test {
public static void main(String[] args) {
Test test = null;
test.func();
}
static void func(){
System.out.println("Hello!!");
}
}
Why this program is getting executed successfully?
This is because static methods are not related to instances. Compiler internally convert this and call Test.func()
You call a static method. Static methods are invoked on Classes.
The call is like this Test.func().
Tip: In your code when you call static methods. Call them Class.method() not on object.
Related
This question already has answers here:
"Error: Main method not found in class MyClass, please define the main method as..."
(10 answers)
Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args) [duplicate]
(5 answers)
legal main method signature in java
(8 answers)
Closed 2 years ago.
public class avarage
{
public void main(int subjectmarks)
{
System.out.println("Eneterd subject marks:"+subjectmarks);
}
}
The compiler shows the above error because it cannot find the native main function signature. Just declare the integer subjectmarks inside the main function and do not pass it as arguments in the main function, like below for example:
public class avarage {
public static void main(String[] args) {
int subjectmarks = <some-value>;
System.out.println("Eneterd subject marks:"+subjectmarks);
}
}
This question already has answers here:
Is it possible in Java to Invoke another class' main method and return to the invoking code?
(5 answers)
Closed 3 years ago.
I have a question in passing a parameter to a public static void main class.
Basically I have the following main class here:
public class testClass{
public static void main(String[] args) {
try {
...//code
String clientId = "test" + args0;
...
} catch(Exception e) {
e.printStackTrace();
}
}
Now I want to pass an id as args0 for the clientid. My attempt is to write a step definition like so:
When I enter the clientId "xxxxxx"
Then in my step def actually call on the main method but it's the below I am having trouble with. I know how to pass parameters from one method to another but how do I pass it in a main where we require an args?
#When("^I enter the clientId \"([^\"]*)\"$")
public void ientertheClientId(String clientId) {
testClass.main(clientId);
}
Above gives me a red line for the method
The function ientertheClientId must be static to call the function main.
This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 3 years ago.
i don't know why there is this error. it want me to add static in my adapter in this code
void setListMovie(ArrayList<Movie> listPresident) {
this.listPresident = listPresident;
}
but when i add static in it. 'this' in my code become error.
this it the my main code that want to add static the adapter class code
ListMovieAdapter.setListMovie(list);
please help why there is this error and how to fix it
Declare your method with static key word
public static void setListMovie(ArrayList<Movie> newMoviews) {
listPresident = newMoviews;
}
and should declare listPresident as static variable
This question already has answers here:
"Non-static method cannot be referenced from a static context" error
(4 answers)
Closed 6 years ago.
I'm using eclipse as IDE for java.
I wrote the following code, but I have one error on loadStrade(). Eclipse suggested me to change loadStrade from public void to public static, and I don't understand why?
I've looked for similar problem and I've found some problems like mine, but I still not understand why I have to change method to static. Uffa!
In the code, routesNet is a graph (jgraphT), and loadStrade() is used to populate vertex and edge.
Can I have help. Thanks, Fabrizio
public class GestioneStrade {
private Stradario routesNet;
public static void main(String[] args) {
/*
* Instantiate Stradario and fill it with routes and cross
*
*/
GestioneStrade m = new GestioneStrade(); //istance of gestionestrade ok?
// now I set new routesNet
m.setRoutesNet(new Stradario());
loadStrade(m.getRoutesNet()); // why loadStrade must be static :-(
}
public Stradario getRoutesNet() {
return routesNet;
}
public void setRoutesNet(Stradario routesNet) {
this.routesNet = routesNet;
}
public void loadStrade(Stradario str) {
// some code to fill routesNet
}
In "main" you should replace
loadStrade(m.getRoutesNet());
to
m.loadStrade(m.getRoutesNet());
And leave loadStrade as non-static.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Understanding which constructor is chosen and why
Why compiler acts like this,
public class Calculator{
private Calculator(Object o) {
// code goes here
}
private Calculator(double[] calc) {
// code goes here
}
public static void main(String[] args) {
new Calculator(null);
}
}
This program executes second constructor. Why first constructor not execute?
Both constructors are accessible and applicable.
The constructor Calculator (Object) accepts any parameter passed to Calculator (double[]), so Calculator (Object) is less specific.