Results 1 to 20 of 23
Thread: Java Class declaration
- 07-15-2010, 04:39 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
Java Class declaration
Hi,
I'm a very beginner in java and I don't want to have questions, that's why I'm asking:
One class can be declared as static or private or nothing.
If you don't declare it as public or private (eg. class myclass {...})
what type is it?
And why in an example program that I have, the compiler complains if I declare it as public or private but it's ok if I don't put anything in front of its name?:)
- 07-15-2010, 06:45 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Then the class have default access. Any class, field (variable), method or constructor that has no declared access modifier is accessible only by classes in the same package, not from the different packages. The default modifier is not used for fields and methods within an interface. If you can go to the Suns' tutorial, you can find a lot about those access modifiers.
- 07-15-2010, 07:10 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Read the following page. Hope you can have a basic idea about those access modifiers in Java.
- 07-16-2010, 08:55 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
Thanks for the reply.
I have the following simple program:
class testClass {
public static void main(String[] args) { int i = 1; }
}
I compile it and it's ok.
If I make the class public or private I have (different) compilation errors.
I can understand these kind of errors but it's obvious that java doesn't treat to the class neither as private nor as public.
Eranga, what following page do you mean?
- 07-16-2010, 10:38 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
You shouldn't get any compilation errors for setting that class public.
- 07-16-2010, 10:48 AM #6
Hi vermio
You can only have a single public class in a java source file.
Java Code:public class Main { public static void main(String[] args) { int i = 1; } }
Java Code:public class Main { public static void main(String[] args) { int i = 1; } } public class Oops { }
Java Code:...\Main.java:7: class Oops is public, should be declared in a [COLOR="RoyalBlue"]file named Oops.java[/COLOR] public class Oops { ^ 1 error
Thanks
TimLast edited by tim; 07-16-2010 at 10:50 AM.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 07-16-2010, 11:46 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
FileName: pr01.java
Public class testClass {
public static void main(String[] args) { int i = 1; }
}
pr01.java:1: class testclass is public, should be declared in a file named testClass.java
Understood
Private class testClass {
public static void main(String[] args) { int i = 1; }
}
pr01.java:1: modifier private is not allowed here
Understood
class testClass {
public static void main(String[] args) { int i = 1; }
}
No errors
I think it's because it is considered as a package? Am I right?
(I haven't studied about packages yet).
- 07-16-2010, 12:34 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Well, it all stems from the filename being different.
You can have as many non-public classes defined in a java file as you want. They will only be visible to other classes in that package. It is not to do with it considering the class a package.
You can only have one public class defined (ignoring inner classes here), and no private classes.
- 07-17-2010, 04:26 AM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
- 07-17-2010, 09:17 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 07-20-2010, 06:20 PM #11
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
if you done specify access modifier then class is package public means that class is accessible withing the package. where as public class is accessible outside the package also.
i hope i have cleared ur doubt.
Hema26
- 07-20-2010, 06:23 PM #12
Member
- Join Date
- Jun 2010
- Posts
- 8
- Rep Power
- 0
hi
if u declare the class public then u must save ur program with same name you have given to ur class. e.g
if u have declared a class Student as public then u must save ur program with same name i.e. Student otherwise it will give error.
regards
Hema26
- 07-20-2010, 06:33 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Txt spk is n abmntn.
- 07-23-2010, 01:27 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
thank u all guys.
- 07-24-2010, 05:24 AM #15
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
If you've solve the problem please mark the thread solved.
- 07-26-2010, 11:18 AM #16
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
Eranga,
sorry, but how can I do this?
- 07-28-2010, 06:19 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
On top of the thread you can see a menu. Click on thread tools, and on the drop down menu list you can find an option to mark the thread solved.
- 07-28-2010, 06:32 PM #18
If a class doesn't have a modifier, what implicit modifier does it have?
Java Code://ERROR public class SomeStuff { } public class NotSomestuff { }
Java Code://ERROR public class SomeStuff { } private class NotSomestuff { }
Java Code://ERROR public class SomeStuff { } protected class NotSomestuff { }
The question is, what implicit modifier does a class have if no modifier is identified?"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 07-28-2010, 06:52 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
It's package protected (or whatever the offical term is).
That is, only things in the same package can access it.
I originally thought you could have protected, but you're right, that's not the case.
- 07-29-2010, 03:59 AM #20
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 13
package private is the proper term :) package protected would infer that sub-packages can access it, and I'm quite sure that isn't the case.
As for why your getting errors, your class is named testClass, and the file is pr01.java. The file should be testClass.java, or the class should be named pr01. (I think you got that, but just to be sure...)If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
class Declaration
By mahtab in forum New To JavaReplies: 1Last Post: 11-01-2009, 07:49 AM -
E:\IT 215 Java Programming\public class Inventory.java:39: class, interface, or enum
By tlouvierre in forum New To JavaReplies: 14Last Post: 05-28-2009, 06:44 AM -
global declaration in java
By gpveena20 in forum New To JavaReplies: 13Last Post: 02-12-2009, 11:25 PM -
whats wrong with this class declaration?
By blossompark in forum New To JavaReplies: 5Last Post: 11-30-2008, 03:18 AM -
Declaration
By asifahmed in forum New To JavaReplies: 1Last Post: 04-05-2008, 06:38 AM
Bookmarks