Strange exception, JDK 6.0.29 bug? - java

Was experimenting with interface inheritance and found this:
interface String{}
interface Object{}
interface Exception{}
interface Integer extends String, Object, Exception{}
public class Test implements Integer{
public static void main(String[] args) {
System.out.println("Test");
}
}
it's compiling, but throws "main" java.lang.NoSuchMethodError: main at runtime?

That's because main method signature should be:
public static void main(java.lang.String[] args) {
if you write
public static void main(String[] args) {
it gets an array of your String interfaces, so the signature is different

If you commend out your inadvisable String interface, the Test class tries to extend java.lang.String, which is a final class. You can't extend it. And asking an interface like your Integer to extend a class instead of another interface should draw a compiler error.
I see nothing educational about this experiment. None of it should ever find its way into real code.

Related

why the main method is printing nothing in the sub class?

"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
}
}

Groovy - Main method placement

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.

How can I instantiate an abstract class to remove a NullPointerException?

So the question title is a little weird but it is the only way I could think of the get the point across. I have a NullPointerException from doing this (The extra code is taken out):
public abstract class Generator extends SimpleApplication{
private static SimpleApplication sa;
public static void Generator(){
CubesTestAssets.initializeEnvironment(sa);
}
I know that the private static SimpleApplication sa; is null but when I instantiate it I get this error instead of a NullPointerException :
SimpleApplication is abstract; cannot be instantiated
I am using the jMonkey Engine 3. If anyone knows how I could solve this problem, that would be great! Thanks!
If you take a close look at the JMonkey documentation, SimpleApplication is made to be extended by you to create your application.
You should create a new class that extends SimpleApplication and implement the missing methods. You then instantiate your custom class and pass it as a parameter.
A little like this :
public class myCustomSimpleApplication extends SimpleApplication {
// Implementing the missing methods
#Override
public void simpleInitApp() {
}
#Override
public void simpleUpdate(float tpf) {
}
// etc...
}
And then...
private static SimpleApplication sa = new myCustomSimpleApplication();
public static void Generator(){
CubesTestAssets.initializeEnvironment(sa);
}
As others have commented, your base application class (I assume it is Generator) which extends SimpleApplication must not be abstract, i.e. it should implement, at least, the simpleInitApp method. See the JMonkey documentation for SimpleApplication or this basic example.
For completion, I will mention that if you really needed to create a dummy instance of an abstract class, you can do so "inline", without explicitly writing the full implementing class. You just need to write the implementation of the abstract methods enclosed by curly brackets after the constructor invocation:
public abstract class AbstractClass {
private static AbstractClass a = new AbstractClass() {
#Override
public void abstract_method() {/*implementation*/}
};
public abstract void abstract_method();
}
An anonymous class is created behind the scenes. But I definitely don't think this is what you need in this case.

Getting strange java.lang.NoSuchMethodError error

I have an interface called Namable
public interface Namable { public String getName(); public void setName(String name); };
Several classes will implement this.
When I make code like this:
Namable foo = new X(); foo.getName();
(X is an implementing class)
I get the error:
java.lang.NoSuchMethodError: Namable.getName()Ljava/lang/String;
This is very strange to me and I'm wondering why this is happening, any thoughts?
Compilable example:
Namable class:
public interface Namable
{
public String getName();
}
Extending class X:
public class X extends java.util.ArrayList implements Namable
{
private String name;
public X()
{
this.name = "bar";
}
public String getName()
{
return name;
}
}
Testing class:
public class Test
{
public static void main()
{
Namable foo = new X();
foo.getName();
}
}
After running this, I get the same error. I am using BlueJ
I have run an example with the classes you have given here and it works as expected once the main method is changed to:
public static void main(String[] args)
However, as you are using BlueJ this may not be a problem (I remember BlueJ using a special mechanism to run classes).
Do you have another class Nameable in your classpath? Check the package import and make sure it is the interface you have defined. It appears the error is with your environment and not with the code.
You need to call
foo.getName()
not
X.getName()
You are trying to call a class (static) method that doesn't exist.
And it should be Namable foo = new X(); - your example code shouldn't even compile as shown.
Can you provide a SSCCE ?
Updated following corrections and SSCCE: Just a wild guess now, but does BlueJ define a Namable already, or do you have a Namable in another package that might be imported instead? What happens if you rename Namable to Namable2 throughout, in the example above?
As stated by DNA your post would be better with a SSCCE
My guess would be that maybe you could have missed a "static" keyword in main function
public static void main(String args[])
not
public void main(String args[])
Could be something a classpath problem too... Or a file name and class mismatch. Or something else.
When I ran your code snippet, the error shown is
Error: Main method not found in class Test, please define the main
method as: public static void main(String[] args)
Change your main method to
public static void main(String[] args)
I am not getting
java.lang.NoSuchMethodError: Namable.getName()Ljava/lang/String;
with your given code snippet.

Main method in a static inner class.?

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()");
}
}

Categories

Resources