#java not detecting main function
1 messages · Page 1 of 1 (latest)
Detected code, here are some useful tools:
class Parent {
int a = 10;
}
class Child extends Parent {
int b = 10;
public static void main(String[] args) {
Child c = new Child();
System.out.println(c.a * c.b);
}
}
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
class Child extends Parent {
int b = 10;
public static void main(String[] args) {
Child c = new Child();
System.out.println(c.a * c.b);
}
}
class Parent {
int a = 10;
}
this work
Detected code, here are some useful tools:
class Child extends Parent {
int b = 10;
public static void main(String[] args) {
Child c = new Child();
System.out.println(c.a * c.b);
}
}
class Parent {
int a = 10;
}
it does work. ur likely just running it incorrectly
show how u execute it please
and then also share the errors u get
@covert harbor
make the class with the main method public and name the file after the public class
huh, maybe move Parent under the Child class
see this is working correctly that's why i asked
does java requires class with main function on top
java version for reference
I dont think though
but not sure
but you shouldnt really have multiple top level classes in one file anyways
just one public class with same name as the file
yeah but this should work right?
not sure how this example was given but they might mean that they are in different files
let me test it
see there is two different class but this is working
other way parent should be on top
then its not working
seems like class with main method needs to be on top
wasnt aware
yeah that's why i'm thinking is there any reference or rule where it says that
its bc of the shorthand syntax java Foo.java
divide it into the full commands and it works
javac Foo.java
java Child
note how javac takes the name of the file, not classes
and java then takes the name of the class
the shorthand syntax was introduced for small one class programs only, as convenience
it doesnt work on more complex setups
i dont think this particular part is explained in the jls, but in the javac specification instead
since that has nothing to do with the language itself, but rather how they decided to design their program apis
yeah this is working thanks
yeah it does