#java not detecting main function

1 messages · Page 1 of 1 (latest)

covert harbor
#
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);
  }
}

why this doesn't work and

supple parrotBOT
#

<@&987246399047479336> please have a look, thanks.

supple parrotBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

covert harbor
#
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

supple parrotBOT
ornate hamlet
#

it does work. ur likely just running it incorrectly

#

show how u execute it please

#

and then also share the errors u get

wraith chasm
#

@covert harbor

covert harbor
wraith chasm
#

make the class with the main method public and name the file after the public class

covert harbor
wraith chasm
#

huh, maybe move Parent under the Child class

covert harbor
#

does java requires class with main function on top

#

java version for reference

wraith chasm
#

I dont think though

covert harbor
wraith chasm
#

but not sure

#

but you shouldnt really have multiple top level classes in one file anyways

covert harbor
#

yeah i'm learning in college

#

they have this example

wraith chasm
#

just one public class with same name as the file

covert harbor
#

yeah but this should work right?

wraith chasm
#

not sure how this example was given but they might mean that they are in different files

#

let me test it

covert harbor
wraith chasm
#

works fine like this

#

even without public class it works

covert harbor
wraith chasm
#

then its not working

#

seems like class with main method needs to be on top

#

wasnt aware

covert harbor
#

yeah that's why i'm thinking is there any reference or rule where it says that

wraith chasm
#

probably in the jls (java language specification)

#

800+ pages

ornate hamlet
#

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

covert harbor
#

yeah this is working thanks

deep plaza