• Post Reply Bookmark Topic Watch Topic
  • New Topic

Collections  RSS feed

 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have a little question about a part of collections.

The thing i am wondering about, is how to make the method fill() in the class TheList, private.
If you can give me a hint or maybe where i can find such info I would be very pleased.






thanks in advance,
Kenneth
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by removing the keyword static from everywhere in your TheList class.
Yes, you can make the fill() method private. All you have to do is write private. But why?
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the quick response,

I already tried to remove static and change public into private, but then i cant call the fill()-method.(fill(List) has private acces in TheList-class)
Is there no good reason to make this method private?
I was teached to make as much private as possible, for security reasons.
So I was wondering how to do this with an arrayList and ended up just trying it out.

Kenneth
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the fill() method is supposed to be called from outside the class and its package, then it ought to have public access. That is what I meant by why? Why do you want a private method?
Access modifiers are not there principally for security. They are there to maintain data integrity. You can alter the data in the object, but only via methods. Calling something static means it does not belong to the object, but to the class. You almost certainly do not want things static in a List class.
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, i understand now there isn't any reason to make the fill()-method private.
The only methods I could think of to make private, are the methods, for instance, to check if a name is spelled right or something like that, but then you wouldn't have to call it in the main method.
And the static means that you want it to remain the same for every instantiated object?
thank you very much for the help
I am still learning, and i must say this forum has already helped me a lot.

Best regards,
Kenneth
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, static really means there is one copy per class. If you make something static, every instance of the class “sees” the same variable. Not copies, not identical variables, but the same variable. So if one instance changes it, all the other instances “see” that change.
Why are you putting private methods into a List to verify spelling? That is not part of the standard functionality of a List, and should probably be implemented elsewhere.
 
Kenneth Van Gysegem
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I understand static! thanks a bunch.

I think you misunderstood a little, I'm not putting the methods in the List. I'm referring to private methods in general.
for instance, I make the method private that verifies the spelling of an input, before it goes into the List.

Thank you again for the replies,
Kenneth
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Van Gysegem wrote:
I think you misunderstood a little, I'm not putting the methods in the List. I'm referring to private methods in general.
for instance, I make the method private that verifies the spelling of an input, before it goes into the List.


It kind of still sounds like you're talking about defining that validation method in the List class, which would not be the appropriate place for it. But yes, an input validation method is one example of the kind of method we might make private.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Van Gysegem wrote:Hi guys,

I have a little question about a part of collections.

The thing i am wondering about, is how to make the method fill() in the class TheList, private.
If you can give me a hint or maybe where i can find such info I would be very pleased.


If you make fill() private, you can't call it from Lijst class. But you have one way..!



You can call "callFill()" from Lijst class now and fill method is private..!!
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that spelling verification method can well be private.
And … “you’re welcome”
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know another way to give the answer.. That's why post that..!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!