hi,

my problem is that :

let i m declaring a package P1 and i have defined one class as public in it.now i know that i can use it anywhere. but let if i want to make a second class as public then it can not be possible.but if i not declare it as public then it can not be used outside package. then what is its use to define a class without public? and what is the way to define two classes in same package and also use them outside package?

by declaring them both as public.
what you are describing is not true:
you can have unlimited classes in every package, and they can all be public, protected, package, ... scope
it looks to me like you're mistaken with: having multiple (inner-)classes in one file.
it is true that you can have only one public class for each .java file, even though you can have several classes in them.

by declaring them both as public.
what you are describing is not true:
you can have unlimited classes in every package, and they can all be public, protected, package, ... scope
it looks to me like you're mistaken with: having multiple (inner-)classes in one file.
it is true that you can have only one public class for each .java file, even though you can have several classes in them.

hey what are you saying in last 2 lines? means i have to make every .java file for a class if i want to make it public ? means i have mistaken by making 2 classes as public in a same .java file ? right ?

every java class must be coded in a .java file (which you compile to .class files)
you can have several classes in a single .java file, but there can be only one public class in a .java file, and that class must have the exact same name as the .java file itself.

... means i have to make every .java file for a class if i want to make it public ? means i have mistaken by making 2 classes as public in a same .java file ? right ?

Yes. Every public Java class must be in its own file.

Don't worry about having too many files. A package is a directory. And each directory can hold a large number of files. It's typical on most projects for each package to contain some number of files. Often a large number of files. The only real limit is that it gets annoying to find the right one where there are a great many files in it.

every java class must be coded in a .java file (which you compile to .class files)
you can have several classes in a single .java file, but there can be only one public class in a .java file, and that class must have the exact same name as the .java file itself.

no no! my question was different. i m asking that if i want multiple public classes in a package then i have to make every different .java file for that particular class. right ?

Yes. Every public Java class must be in its own file.

Don't worry about having too many files. A package is a directory. And each directory can hold a large number of files. It's typical on most projects for each package to contain some number of files. Often a large number of files. The only real limit is that it gets annoying to find the right one where there are a great many files in it.

.

Yes. Every public Java class must be in its own file.

Don't worry about having too many files. A package is a directory. And each directory can hold a large number of files. It's typical on most projects for each package to contain some number of files. Often a large number of files. The only real limit is that it gets annoying to find the right one where there are a great many files in it.

when i declare a class a protected in a package then at compiling time it says that modifier protected not allowed here ? why it is so ???

it is not.
I think you may miss something somewhere. as for your previous question, I did say:

there can be only one public class in a .java file, and that class must have the exact same name as the .java file

it is not.
I think you may miss something somewhere. as for your previous question, I did say:

package P1;

protected class D
{
public int multiply(int a,int b)
{
return a*b;
}
}

this is class in package P1. P1 has one public class also nothing else. now i m n compiling this. it is saying not allowed here this modifier

ah, OK, I think I misunderstood you there.
yes, you can only put either 'public', 'final' or 'abstract' there (or a combination thereof)
if you want to put protected, you should place it as an access modifier on the classmembers (being the variables and/or methods, depending on which you want to be protected)
an inner class, on the other hand, can be set as 'protected'

For a class in its own file, you can only use one of these two protections: "public" and "package protected". "Package protected" is indicated by omitting the "public" keyword; there is no keyword for "package protected."

D.java:

package P1;
class D {
	public int multiply(int a, int b) {
		return a * b;
	}
}

"private" and "protected" can only be used within a class. So you can do this...

C.java:

package P1;
public class C {
	protected static class P {
		public int multiply(int a, int b) {
			return a * b;
		}
	}
}

But then you use the class as "C.P".

For a class in its own file, you can only use one of these two protections: "public" and "package protected". "Package protected" is indicated by omitting the "public" keyword; there is no keyword for "package protected."

D.java:

package P1;
class D {
	public int multiply(int a, int b) {
		return a * b;
	}
}

"private" and "protected" can only be used within a class. So you can do this...

C.java:

package P1;
public class C {
	protected static class P {
		public int multiply(int a, int b) {
			return a * b;
		}
	}
}

But then you use the class as "C.P".

hmm thats right! this is really very well answered. !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.