Static Methods in Private class - java

I have recently started learning java and its my first OOP language.I read that static methods do not require the class to instantiated they run when you feed the class to the JVM.My question is what would happen if the static method is inside a private nested class. will it still run?
EDIT- I tried it does not work, I want to know what is happening in the background.
public class tester{
private class estupid{
public static void main(String[] args){
System.out.println("Hello Im a static method of a private class and main too");
}
}
}
To the people down voting, A suggestion,A more productive activity would be telling what's wrong with the snippet, Thank You.

There are many mistakes, which you can simply get by compiling the code. I suggest you to use command line javac compilation
If you compile your code as it is
C:\src>javac tester.java
tester.java:3: error: Illegal static declaration in inner class tester.estupid
public static void main(String[] args) {
^
modifier 'static' is only allowed in constant variable declarations
1 error
As per above error, make your nested class as static nested class. Now the code will compile successfully but you will get error while running it:
C:\src>javac tester.java
C:\src>java tester
Error: Main method not found in class tester, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
With above error you can understand that you are running tester class, but it does not contain any main method which is searched by the JVM. So add a main method in tester class and yes you can call the static method in static inner class. Changed code will be like this which will run properly:
public class tester {
private static class estupid {
public static void main(String[] args) {
System.out.println("Hello Im a static method of a private class and main too");
}
}
public static void main(String[] args) {
estupid.main(args);
}
}
After compiling and running the above code
C:\Himanshu\GitHub\hsingh-learning\src>javac tester.java
C:\Himanshu\GitHub\hsingh-learning\src>java tester
Hello Im a static method of a private class and main too
This is just to correct your code and making it compilable and runnable, BUT writing main method in nested class is not suggested.
Other thing is that you are making private nested class, so you are making it unaccessible from outside of the holding class (tester class in your case). tester class is public and accessible to JVM but the nested class is marked private so that cannot be accessed.
That does not mean you cannot invoke main static method of nested class from JVM. Make your nested class public.
public class tester {
public static class estupid {
public static void main(String[] args) {
System.out.println("Hello Im a static method of a private class and main too");
}
}
}
Compile it, which will produce 2 class files.
1. tester.class
2. tester$estupid.class
Run the 2nd tester$estupid which contains the main method (which is required by JVM)
C:\Himanshu\GitHub\hsingh-learning\src>java tester$estupid
Hello Im a static method of a private class and main too

The main method must be a member of a public class. A static method is a method that is a child of the class itself rather than an object, or an "instance" of that class.

Related

Unsure as to why there is a class, interface or enum expected error?

I have just started creating this program and I am trying to make a method outside of the main class. I researched the error and it says there are too many curly brackets outside the main function.
I was just wondering if it was actually possible to have a public static function outside of a main class?
class NewMain {
public static void main(String[] args) {
}
}
public static void plusOne(){
}
Think of a class as a collection of code. You can have as many classes as you want. But you can not put code outside of a class and there is no reason you should want to.

After accessing static methods in static inner classes from main class in java, eclipse is giving warning

I have a class -->
public class Machine
There I have declared a static inner class -->
public static class Parts
Inside static inner class I have declared two static methods -->
public static void engine()
public static void battery()
Now I want to access the methods from my main class App. I am using Eclipse IDE. I did -
Machine.Parts machine = new Machine.Parts();
machine.engine();
machine.battery();
Eclipse is letting me to do it. But it is giving me warning -
The static method engine from the type Machine.Parts should be accessed in a static way
The static method engine from the type Machine.Parts should be accessed in a static way
How to resolve this problem?
I have tried google search and stack overflow previous questions. But nowhere I could find the solution.
My code -
Machine.java -->
package demo28;
public class Machine {
public static class Parts {
public static void engine() {
System.out.println("Machine engine is running");
}
public static void battery() {
System.out.println("Machine battery is charging");
}
}
}
App.java -->
package demo28;
public class App {
public static void main(String[] args) {
run(new Machine.Parts());
}
public static void run(Machine.Parts machine) {
machine.engine();
machine.battery();
System.out.println();
}
}
Output -->
Machine engine is running
Machine battery is charging
Expected --> No warning
Actual --> Getting warning
Here:
Machine.Parts machine = new Machine.Parts();
You are creating an instance of that inner class. Then you go:
machine.engine();
... invoking a static method, as if it were a non-static method of that class.
Yes, the above code works, but it is bad practice. If you meant to have "real" non-static methods, simply drop that keyword from the method signatures. Otherwise, change your code to:
Machine.Parts.engine();
Because that is what really happens in your code example.

Can we create an object of the class in which main function is defined in Java?

Is it possible to create object of class in which main method resides.I have been searching for this answer but I have been told that it depends on compiler some compiler will allow while others will not.Is that true?
Yes? The main method is just an entry point. The class is like any other, except it has an additional public static method. The main method is static and therefore not part of the instance of the object, but you shouldn't be using the main method anyway except for starting the program.
public class Scratchpad {
public static void main(String[] args) {
Scratchpad scratchpad = new Scratchpad();
scratchpad.someMethod();
}
public Scratchpad() {
}
private void someMethod() {
System.out.println("Non-static method prints");
}
}
Yes, you can create object for the class which has main method. There is no difference in this class and a class which don't have main method with respect to creating objects and using.

Can't call a static method from another class to main class

I created a static method which returns a static int in class NewTriangle. But when I try to call it from the main it won't print. It asks to create a method with the same name inside the main.
public class NewTriangle{
public static in numberOfTriangles;
public static int getnumberOfTriangles(){
return numberOfTriangles;
}
}
The code works up until this point. But When I call getnumberOfTriangles() from the main I get an error.
public static void main(String args[]){
System.out.println(getnumberOfTriangles());
}
If your main method is in a different class, then you need to specify the classname while calling the static method, i.e., NewTriangle.getnumberOfTriangles()
Assuming the typos in your code are copy and paste errors, you need to either
Use the class name before the method name, you may need to add the class to your import statements (you need to if it is in a different package)
System.out.println(NewTriangle.getnumberOfTriangles());
Add a static import of the getnumberOfTriangles method to your main class
import static NewTriangle.getnumberOfTriangles;
However, note the caution given in the link:
So when should you use static import? Very sparingly!
You have to write the name of the class before :
NewTriangle.getnumberOfTriangles()
You can do something like this:
Example using :
I have created a package name stackoverflow
Two class callerClass and NewTriangle in package stackoverflow.
Make an import of class NewTriangle in callerClass.
using the Static function by NewTriangle.getnumberOfTriangles()
NewTriangle.getnumberOfTriangles() // className.StaticfunctionName()
Working Code:
Class 1 :
package stackoverflow;
public class NewTriangle {
public static int numberOfTriangles;
public static int getnumberOfTriangles() {
return numberOfTriangles;
}
}
Class 2 :
package stackoverflow;
import stackoverflow.NewTriangle;
public class callerClass {
public static void main(String args[]) {
System.out.println(NewTriangle.getnumberOfTriangles());
}
}
Java static method :
If you apply static keyword with any method, it is known as static method.
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.
I hope I was helpful :)

what is wrong with this code public class Hello { public static void main() { System.out.println("Doesn't execute");

I typed this code into Eclipse
public class Hello
{
public static void main()
{
System.out.println("Doesn't execute");
}
// .....
}
When I press run it says that it does not contain a main type.
I don't know what I'm doing wrong , and I am new to java.
It should be:
public static void main(String[] args)
This is what your code should look like:
public class Hello {
public static void main(String[] args) {
System.out.println("Doesn't execute");
}
}
Notice the closing parenthesis, also I have properly changed your main method.
Here's another hint:
When you create a new Java class in Eclipse, there is an option to auto-generate the main method stub for you (this option would have fixed your error without you even knowing).
It is the first checked checkbox in the following screenshot.
main method without the string array arguments is not the method that JVM looks for to start the execution of a class.
After completion of the initialization for a class (during which other consequential loading, linking, and initializing may have occurred), the method main of class is invoked.
The method main must be declared public, static, and void. It must specify a formal parameter whose declared type is array of String. Therefore, either of the following declarations is acceptable:
public static void main(String[] args)
public static void main(String... args)
Read more about JVM startup, loading, linking and intilization of class here:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.1.4

Categories