This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes public and private? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public and private?" Watch "public and private?" New topic
Author

public and private?

colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

I do not understand this error
the constructor name is:

public ShapeIcon(URL picHome) {

and the error I get is:


I only said public so why is it saying that I said both?
[ March 29, 2008: Message edited by: colton peterson ]

www.mormon.org
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Do you only have one public, top-level class per source-file?

If that's not the problem, will you post your entire source-file so that we can see the code in context?
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
I was guessing the class was private and the constructor public?


Dale Carnegie:<br />"Most of the important things in the world have been accomplished<br />by people who have kept on trying when there seemed to be no hope at all."
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

He is right. I guess I have to declare class public for constructors to be public? That would make sense I guess . . .



Thanks!

Oh, and do you need the class to be public to have public methods and variables as well?
[ March 29, 2008: Message edited by: colton peterson ]
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

Never mind. I marked my class public but same error message.

Here is my entire class, it is the only one in its file.

It makes an ImageIcon and keeps a rectangle that has the exact same
coordinates and size.

Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54


Carfully look under this line



You have the word private there so your class is private public
[ March 29, 2008: Message edited by: Stuart Smith ]
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Oh, and do you need the class to be public to have public methods and variables as well?


As I understand it I am new to Java also.

Encapsulation of fields (class variables) promotes security so they should be private and public methods should be written called setters and getters.

i.e
private int somthing;

public setSomthing(int s)
{
this.something = s;
}

public getSomthing()
{
return this.something;
}
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Originally posted by Stuart Smith:
so your class is private public

Only the constructor, but that's exactly the problem here.

I've managed to get this a lot as well, starting to type a field declaration, then for some reason not finishing it. All that's left is the private, which indeed clashes with the access modifier of the next field, constructor or method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Stuart Smith
Ranch Hand

Joined: Mar 28, 2008
Posts: 54
Good spot Rob
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

do you need the class to be public to have public methods and variables as well?


you can't declared the class as private but nested class.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Muhammad Saifuddin:


you can't declared the class as private but nested class.


This doesn't make sense.

The error in the original poster's code has already been found (a stray word "private"), but some other stuff that's not quite right has been said. Just to put it right...

A top-level class can only be public or package access. You can't have a protected or private top-level class.

A nested class can have any access modifier, including private.

The access level of each constructor and method is separate to that of the class. For instance, even a private class can have a public constructor. The number of times you'd want to do that is small (but there are times).


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public and private?
 
Similar Threads
Exception in thread "AWT-EventQueue-2" java.lang.OutOfMemoryError: Java heap space
constructor with return value
Private Access Levels in Inner Classes
on the subject of packaging
Default constructor?