0

Created a new class as per given prototype, but it shows error

public class test { void Test() { System.out.println("First ever class in Core Java"); } } class trya { public static void main(String[ ] args) { test a = new test(); a.Test(); } } ..................................... This code gives the follwing error: Error: Main method not found in class test, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application ............................................................ Even though the defined prototype is : public class Animal { void bark() { System.out.println("Woof-Woof"); } } class MyClass { public static void main(String[ ] args) { Animal dog = new Animal(); dog.bark(); } } which is giving output.

23rd Sep 2018, 4:47 AM
Shivdutt Dixit
Shivdutt Dixit - avatar
6 Réponses
0
Like java complain, you have to add main method to "test" class
23rd Sep 2018, 7:26 AM
KrOW
KrOW - avatar
0
why do I need to add main method in test class??
23rd Sep 2018, 7:39 AM
Shivdutt Dixit
Shivdutt Dixit - avatar
0
When you run a program in Java it will look for the main method in the public class from that file. And also you can only have 1 public class per file. Therefore you have to either change the visibility of the classes or move the method main to the public one! Oh and just a reminder: Classes always start with upper case letter and methods with lower case ones!
23rd Sep 2018, 11:33 PM
Chriptus13
Chriptus13 - avatar
0
How come does the compilation of the protype does not give the error??
24th Sep 2018, 2:43 AM
Shivdutt Dixit
Shivdutt Dixit - avatar
0
Shivdutt Dixit depends in how you're executing, are those 2 classes in the same file?
24th Sep 2018, 6:28 AM
Chriptus13
Chriptus13 - avatar
0
yes they are in the same file
24th Sep 2018, 6:43 AM
Shivdutt Dixit
Shivdutt Dixit - avatar