Public Class Animal Cannot Be Accesed | Sololearn: Learn to code for FREE!

+1

Public Class Animal Cannot Be Accesed

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

1/25/2020 4:46:22 AM

Garvit Joshi

7 Answers

New Answer

+3

please show your code so that you can get the appropriate help you need

+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/

+2

The code works without a problem for me: https://code.sololearn.com/c55GZ7HcZjEO/?ref=app

+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.

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

+1

This Code Is In Solo Learn In Java Class Section.

+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