Public Class Animal Cannot Be Accesed | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Public Class Animal Cannot Be Accesed

Public Class AnImal Cannot Be Accessed By Main Class if They are Written In Same Java Prog.

25th Jan 2020, 4:46 AM
Garvit Joshi
Garvit Joshi - avatar
7 Answers
+ 3
please show your code so that you can get the appropriate help you need
25th Jan 2020, 4:48 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 2
It is not clear from your question that what are you trying to ask. But to use a method of a class, you need to create an object of it and then you can invoke that method. https://www.sololearn.com/learn/Java/2155/
25th Jan 2020, 7:17 AM
Avinesh
Avinesh - avatar
+ 2
The code works without a problem for me: https://code.sololearn.com/c55GZ7HcZjEO/?ref=app
25th Jan 2020, 8:07 AM
Sonic
Sonic - avatar
+ 2
Garvit Joshi Just see whether your main class is also public in that case because java cannot have two public classes in a single file since the public class has to be the source file name then if you declare two public classes then there's ambiguity in choosing a single class as file name.
25th Jan 2020, 8:24 AM
Avinesh
Avinesh - avatar
+ 1
public class Animal { void bark() { System.out.println("Woof-Woof"); } } class MyClass { public static void main(String[ ] args) { Animal dog = new Animal(); dog.bark(); } }
25th Jan 2020, 4:59 AM
Garvit Joshi
Garvit Joshi - avatar
+ 1
This Code Is In Solo Learn In Java Class Section.
25th Jan 2020, 4:59 AM
Garvit Joshi
Garvit Joshi - avatar
+ 1
Sonic ,Avinesh this Program Works Fine In Sololearn IDLE but It Shows An Error if You Do It In Java 8 ... The Error Was Something like Public Class Cannot be accessed you have Make Onther Java File for It ... if I Remove Public Keyword From The Class Name The Error Was Solved
25th Jan 2020, 8:14 AM
Garvit Joshi
Garvit Joshi - avatar