class

Started by
16 comments, last by Stoo 15 years, 9 months ago
I believe the protected keyword in Java actually makes the variables available to the package, not just inheritance hierarchies like some other languages. That aside, there is no reason to add "getters" unless there is some *real need*. You shouldn't add them just because there are private variables. Classes are generally not dumb data holders, and should not be treated as such by default.
Advertisement
heya,

thanks for all the input i am still learning a fair amount at the moment i am learning about arrays and how to input data that can be recalled using different elements of the array and then printing them out in the console.

I am actually finding it interesting and I'm not sure if i am learning the best language for my first one apart from a dabble in Python but i find Java somehow easier to grasp.

Thanks for all the input once again

vel
Quote:Original post by Zahlman
Quote:Original post by ukdeveloper
It's been a while since I last used Java but IIRC it doesn't support friend classes or methods. Having private variables is arguably better OO practice but how do you access them without a getter if they're private?


How am I supposed to check your e-mail for you without your password?


It's just the impression I got from the way rip-off said it, in that he said you didn't always need getters; I misunderstood him and assumed that there was some crazy way to access private variables without them in Java even though it doesn't support friend functionality.

But anyway, I think rip-off might be right about the protected keyword giving a whole package access to a variable but I really have better things to do than to check stuff like that.

Crappy geek chat-up line #378923789479^e = "I lost my password, can I have yours?"
errrm how is that a chat up line sorry if i sound stupid lol i don't get it what so ever XD

Vel
Quote:errrm how is that a chat up line sorry if i sound stupid lol i don't get it what so ever XD
All will become clear when you are older.
Quote:Original post by rip-off
Quote:
You have a bunch of "setters" there already, but making the variables private requires you to add some "getters" there too.


This is most certainly not true.



There is no way of accessing private class members directly from outside the class.

Thats the whole point of it.
If you had a variable, that can have values from 0 to 12 and you declared it public, or even protected, there is nothing preventing code from other classes from changing the value into something invalid.

By using accessors, you can make sure the variable can only be given decent values.


public void setArraySize(int newSize)
{
if(newSize < 1)
{newSize = 1;} // No negative or zero arrays allowed.
else
this.arraySize = newSize;
}


Defensive programming is the way to go.

[Edited by - Stoo on July 7, 2008 8:50:19 PM]
Quote:Original post by Stoo
public void setArraySize(int newSize)
{
if(newSize == 1)
{newSize = 1;} // No negative or zero arrays allowed.
else
this.arraySize = newSize;
}


Aye... If newSize == 1, then newSize = 1? [wink]

As for what rip-off was saying, it makes perfect sense. Just because you have a private variable doesn't mean you need a setter and/or getter. Usually when it's private, it's meant to be used by the class itself, not by some outsider. Otherwise you'd make it public. Just as Zahlman said, your email password is private such that you're the one using it - if everyone was meant to use your email, your password would be public. A getter/setter here wouldn't make your password any more secure [wink].
Quote:Original post by agi_shi
Quote:Original post by Stoo
public void setArraySize(int newSize)
{
if(newSize == 1)
{newSize = 1;} // No negative or zero arrays allowed.
else
this.arraySize = newSize;
}


Aye... If newSize == 1, then newSize = 1? [wink]

As for what rip-off was saying, it makes perfect sense. Just because you have a private variable doesn't mean you need a setter and/or getter. Usually when it's private, it's meant to be used by the class itself, not by some outsider. Otherwise you'd make it public. Just as Zahlman said, your email password is private such that you're the one using it - if everyone was meant to use your email, your password would be public. A getter/setter here wouldn't make your password any more secure [wink].



Heh. That was a hilarious one.. if 1 then 1.
Im not even sure, why I changed the value of the parameter instead of a different approach.

if(value < 1){result = 1}
else {result = value}

If that arraysize from my example was meant for public use, how would you guarantee it could never be set for invalid value? If some value can be used in a wrong way, it sooner or later will be.

Garbage in Garbage out paradigm just doesn't work well these days.

This topic is closed to new replies.

Advertisement