Here is my code:
class cat {}
class dog {
static void main(String[] args) {}
}
When compiled groovy says I do not have a main method. But when I get rid of the cat class:
class dog {
static void main(String[] args) {}
}
Its valid. I thought, as long as I had the main method in any class the code was valid, but I am wrong. Can someone explain why I can not have more than one class when the main method resides in one of the classes?
You can have more than one class, but the class defined first has to have the main method implementation. Normally when run as a script, the script is executed in run() method.
In case you have a class defined, then the name of the class is used as the name of the script. In case there are more than one public class, then the runnable implementation has to be part of the first defined class. Below should work:
class Dog {
static void main(String[] args) {
println "hello"
}
}
class Cat {}
You can get a clear picture when you inspect AST in groovy console.
Related
"mysecondclass" which is a sub class of the super class "myfirstclass"
should inherit all the properties of the super class "myfirstclass"
and it's expected to output the value of x but instead it prints nothing
package myfirstproject;
public class myfirstclass {
protected int x = 10;
//getter
public void getX() {
System.out.println(x);
}
public static void main(String[] args) {
/* it asked me to define this main method here although it's useless in this situation
*/
}
}
class mysecondclass extends myfirstclass {
public static void main(String[] args) {
mysecondclass mysecondobject = new mysecondclass();
mysecondobject.getX(); //prints nothing
}
}
i am really newbie in java and i am actually still learning it's basics
also i do not know why it always asks me to add the main method in the super class although i have already declared one in the sub class (i heard one main method is enough for one java file)
any help is really appreciated , thanks in advance
You are not understanding the function
public static void main(String[] args)
In Java Docs :
The public static void keywords mean the Java 1 virtual machine (JVM)
interpreter can call the program's main method to start the program
(public) without creating an instance of the class (static), and the
program does not return data to the Java VM interpreter (void) when it
ends.
You have two
public static void main(String[] args)
In two different classes! You are running or compiling the one that uses myfirstclass So for this type of example or test is a good practice to have only one Java class Main
public class Main {
public static void main(String[] args) {
// Call Your classes
}
}
Be more clean and organized in your code!
And one other thing. For running by Command Line use this type of commands
java MyApp arg1 arg2
When in doubt always read the Docs!
You have 2 ways to make your code running as you wish
1) create 2 java file. one is myfirstclass.java the second is mysecondclass.java. each hold their main class. Thus you can kick off any main method as you wish.
2) just keep these 2 class in the same java file. then when you running it, it will only run the main method in default in the public class.
Just change the public keyword to the second class. If you are using an online compiler, they usually look for the main method inside the public class.
package myfirstproject;
class myfirstclass {
protected int x = 10;
//getter
public void getX() {
System.out.println(x);
}
}
public class mysecondclass extends myfirstclass {
public static void main(String[] args) {
mysecondclass mysecondobject = new mysecondclass();
mysecondobject.getX(); //printx X
}
}
I want to get the class name from within a static method but without instantiating the class.
The following code snippet is not what I want to achieve because it instantiated the class first.
package a.b.c;
public class Program {
public static void main(String[] args) {
System.out.println(new Program().getClass().getName());
}
}
Did you tried :
System.out.println(Program.class.getName());
I have class that contains only a main method. I want to pass parameters to that method not from the terminal but from another class. How Can i do this?
public class class1
{
public class1{}
}
public class class2
{
public class void main(String args[]){}
}
I want to pass An object of class class1 to the main of class2 . is That Possible??
You can call it as usual.
Example :
public class Test {
public static void main(String[] args) {
System.out.println("Inside Test class main()");
}
}
The other class
public class Test2{
public static void main(String args[]) {
System.out.println("Inside Test2 class main()");
Test.main(args);
}
}
Now run the Test2 class.
Output:
Inside Test2 class main()
Inside Test class main()
There is a reason why they call it the main method. It is the first method that is executed when your application starts. And you cannot call it (as the main method), the JVM does it for you.
Of course you can call it like any other method, but it will not be called in the role of the main method but as any other ordinary static method.
I declared the following class
class A { //not public
public static void main(String args[]) {
System.out.println("done");
}
When I compile and run it, it runs fine and prints the output "done". Same behavior even when I declare it as being in a "package a;"
However, if JVM spec mandates that main method should be public since "it can't see main otherwise", shouldn't it apply to the class as well?
If the JVM "can't see" A.main() when it is not declared public, how is it able to see the class A itself.
Is there any explanation for this other than "because the specification says so"?
The JVM has access to every class in the application all the time because one of its responsibilities is enforcing visibility rules. Therefore, one can draw the conclusion that it can ignore visibility rules if need be (e.g. when the user starts the application, the JVM has to find the entry point, which is main()).
In other words, the JVM is not a class accessing this function, so visibility doesn't apply. It is basically the overseer, managing the application from execution to termination.
For reference, see Execution.
When you declare a class private, you're not making it "invisible", and the same goes for your methods. Declaring a method private simply means it's not callable from outside your class. A static public method of a private class is publicly callable.
The reason the JVM can see a non-public class is because it controls visibility, meaning it sees everything and decides what can see/call/access what.
The use of public on a class is different than on a method, but the concept is the same.
On a method, the public keyword means the method can be used outside the class. An example would be:
class A {
public static void do() {
// Do something
}
}
class B {
public static void main(String[] args) {
A.do(); // This works because do() is public and static
}
}
The same concept applies to classes, but in a different way.
Using public on a class means that it can be used outside the current .java file (it will have its own .class file).
Here's an example:
//C.java
class C {
static void do() {
// Do something
}
public static void run() {
A.do(); // Works because A.do() is public and static
B.do(); // Does not work because B is not a public class
}
}
//A.java
public class A {
public static void main(String[] args) {
B.do(); // Works because B is in the same file
do(); // Duh...
}
public static void do() {
// Do something
}
}
class B {
static void do() {
// Do something
}
}
I've learnt that the only public class in a Java file must also have the main method. However, below you can see the main method inside an inner class instead?
What is the rule with regard to the main method definition in a source file?
public class TestBed {
public TestBed() {
System.out.println("Test bed c'tor");
}
#SuppressWarnings("unused")
private static class Tester {
public static void main(String[] args) {
TestBed tb = new TestBed();
tb.f();
}
}
void f() {
System.out.println("TestBed::f()");
}
}
If you want to start a class with java (the Java launcher: java test.MyClass) then this class must have a main method with the well known signature.
You can have a main method with the same signature anywhere you want. But don't expect that the launcher will find it.
P.S. The name of the language is Java, not JAVA.
There is a minor detail:
You may do this:
package test;
public class Test {
/**
* #param args the command line arguments
*/
static public class A {
public static void main(String[] args) {
System.err.println("hi");
}
}
}
java test.Test$A
but this is non standard ...
Any class that can have a static method can have a public static void main(String[] args).
This includes:
top-level classes (whether public or not), e.g.
public class Foo {
public static void main(String[] args) {
System.out.println("Hello");
}
}
and inner static classes (whether public or not) (like your example).
It does not include:
anonymous classes
inner non-static classes
So both of these are illegal:
public class Foo {
private Object bar = new Object() {
public static void main(String[] args) {
System.out.println("Hello");
}
};
}
public class Foo {
private class Bar {
public static void main(String[] args) {
System.out.println("Hello");
}
};
}
Every Java application must have a main method. It’s the starting point for the execution of the code in the application. Its method signature is:
public static void main(String[] args)
A static inner class is a class that is defined inside of a different class's definition and marked as being static.
For example, if the outer class is named TestBed, then an inner class of TestBed, which is named Tester, would be compiled into TestBed$Tester.class. The separation of .class files means that you can keep the supplemental, nested code tightly coupled to the primary, outer class.
They are in the same source file, and the inner class is actually inside the outer class. All that and you don't have to pay any sort of deployment or run time cost.
By using static inner classes, you can add additional support functionality to your systems for capabilities such as testing, while incurring no penalties in normal, production deployment.
To execute the main() method of that TestBed.Tester class,
% java TestBed$Tester
This is interesting as the code will compile and run in Eclipse, but will just compile from using commmand line. When you run from eclipse it will find the static main method from within the inner class and run it.
But when running java TestBed from the command line you will get error - Exception in thread "main" java.lang.NoSuchMethodError: main which is a valid error as you have not defined your main method in main class.
Why would you want to define your main method in an inner class? Your main method should be defined in public class, this is not a rule but common practice.
In below code I've moved the main method into outer class which works both in Eclipse & command line :
public class TestBed {
public TestBed() {
System.out.println("Test bed c'tor");
}
#SuppressWarnings("unused")
private static class Tester {
}
public static void main(String[] args) {
TestBed tb = new TestBed();
tb.f();
}
void f() {
System.out.println("TestBed::f()");
}
}