• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

public and private?

 
Ranch Hand
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was guessing the class was private and the constructor public?
 
colton peterson
Ranch Hand
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good spot Rob
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
reply
    Bookmark Topic Watch Topic
  • New Topic