The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

now, if I use < String student > and <int sGrade> instead of public String student and private int grade as instance variables in the code above, I'll get the same result!
what good are they for?!
thanks

Re: modifiers for variable

For example, a private field can only be accessed within that class, protected means the field can only be accessed in the same package or a descendant class, and public means it's accessible everywhere.

Why would you want to have different modifiers? To keep nosy developers out. Take the ArrayList class as an example.

Internally, there is a size field. The ArrayList class needs to be able to update this field as the size changes so it can't be declared final. However, at the same time it would be very bad if external code changed the size field, either intentionally or unintentionally. The best solution is to declare the size field private so the ArrayList internals can do whatever it needs with the size field and access can be controlled through accessor methods (a.k.a. getters/setters).

Is it the end of the world if external code does get access to "private" fields? Not really, and some languages completely lack a "hard" accessibility limit (i.e. Python, Javascript). These languages often have "gentleman's agreements" on what you shouldn't and should access on the off chance you really know what you're doing and want to work outside the typical bounds. More often than not, though, you don't want to do this as it usually makes code more obscure and harder to understand. Java takes this approach and enforces the hard limits.

Re: modifiers for variable

I'm not entirely sure what the question is for the first half, could you explain again or show code in a different way that isnt working as expected? Your issue in the second half is that your assignment puts the value of the right side into the value of the left side. So, name and grade start as null and 0 and you are assigning them to s and g which doesnt change name and grade at all. The other way around sets s and g to name and grade which is what you are looking for i think.

Writing code is your job, helping you fix and understand it is mine.

<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Re: modifiers for variable

For example, a private field can only be accessed within that class, protected means the field can only be accessed in the same package or a descendant class, and public means it's accessible everywhere.

Why would you want to have different modifiers? To keep nosy developers out. Take the ArrayList class as an example.

Internally, there is a size field. The ArrayList class needs to be able to update this field as the size changes so it can't be declared final. However, at the same time it would be very bad if external code changed the size field, either intentionally or unintentionally. The best solution is to declare the size field private so the ArrayList internals can do whatever it needs with the size field and access can be controlled through accessor methods (a.k.a. getters/setters).

Is it the end of the world if external code does get access to "private" fields? Not really, and some languages completely lack a "hard" accessibility limit (i.e. Python, Javascript). These languages often have "gentleman's agreements" on what you shouldn't and should access on the off chance you really know what you're doing and want to work outside the typical bounds. More often than not, though, you don't want to do this as it usually makes code more obscure and harder to understand. Java takes this approach and enforces the hard limits.

Ok thanks a lot
So you mean in general, we only use public, private, protected,etc... only when we want to make a class that is going to be used in some other program, or external codes?! in this case, is there any difference between "public String student" and "String student" at all?! since both can be accessed by other external codes...

--- Update ---

Originally Posted by Chris.Brown.SPE

I'm not entirely sure what the question is for the first half, could you explain again or show code in a different way that isnt working as expected? Your issue in the second half is that your assignment puts the value of the right side into the value of the left side. So, name and grade start as null and 0 and you are assigning them to s and g which doesnt change name and grade at all. The other way around sets s and g to name and grade which is what you are looking for i think.

it's a part of my code, but when I run it, it will give 0 for "result" all the time...seems like the variable "result" is always set as 0 or null!
why is it like that?! I assigned the value of variable "intBet" to it earlier!!!

Re: modifiers for variable

I would start by debugging the line wher eyou get playerBet.getText() and see what is coming out of that. Either put a System.out.println() there or a break point (debugging through your IDE, highly recommended you learn how to do this with your IDE). I'm betting (pun intended) that your playerBet is not getting set the way you expect. If not that then maybe you're action playerBetActionPerformed is never getting triggered. Try setting result to some arbitrary number and see if it is ever getting changed to 0 or if it has just been staying at 0.

--- Update ---

Program Tracking Tip: Additionally, when making a program with lots of actions, it can be beneficial to put a System.out.println("<Function name> called") in all of your functions. This will allow you to follow what the program is doing when it runs and quickly find something like "Hey, that function wasnt ever called."

Advanced Logging Tip: I know proper logging tecniques can go over most people's heads. Something i like to do when in a pinch is put the following lines all over the place:

//Put this at the top of your class
static private boolean logging = true;
//Put this all over the place. You can now turn them on and off whenever you want to.
if(logging){
System.out.println("Message here");
}

Writing code is your job, helping you fix and understand it is mine.

<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Re: modifiers for variable

What was the value of intBet? Could it be 0? To see its value, add a println to print intBet's value after it is set.

intBet is the user's input...the program is part of a GUI made by netBeans GUI maker!!! so I wasn't sure I should put it all here!

--- Update ---

@Chris Brown
I see what you mean
I got my mistake by putting System.out.println
Thanks a lot guy
about using debugging of the IDE, I've tried using it, but it doesn't really do anything...my IDE is netBeans, when I press debug project button, there be something like 121/321 mb...and just the numbers change...I'm not sure what debugging does in this IDE!!!

another thing...I've used GUI of netBeans with design area to make this code...is there anyway I could run this code on Eclipse?! or command line?!
it has already some built in codes that I'm not supposed to change!

Re: modifiers for variable

The way java is designed, you should be able to run it anywhere. There shouldnt be anything tying it to your IDE. I understand the thing with netbeans developed GUIs (oh how i hate them). But they dont need netbeans to run.

As for debugging, pressing the debug button simply runs your program in debug mode. You need to set a breakpoint on a line to get the program to stop so you can look at variable and step through code line by line. Check out this tutorial Netbeans Debugger Short Tutorial

Writing code is your job, helping you fix and understand it is mine.

<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!