Results 1 to 4 of 4
  1. #1
    Webstorms is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Explanation of some Java certain aspects

    Hey, I see that Java has keywords like "public", "private" and "final". I know what they do. Public and private mark the accessibility of a certain methode or variable. Final marks a variable as a constant. Now where my confusion comes from, is that, if you know, that you not going to change a certain variable for example, then why use the keyword "final"? To make it clear to read?
    The same for "public" and "private", what's the point -- if you know that you not going to use a certain methode in a different class, why do you have to mark it? I hear Java is a safe language, is this, that makes it safe? Please clear me up. :)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Explanation of some Java certain aspects

    Quote Originally Posted by Webstorms View Post
    Hey, I see that Java has keywords like "public", "private" and "final". I know what they do. Public and private mark the accessibility of a certain methode or variable. Final marks a variable as a constant. Now where my confusion comes from, is that, if you know, that you not going to change a certain variable for example, then why use the keyword "final"? To make it clear to read?
    The same for "public" and "private", what's the point -- if you know that you not going to use a certain methode in a different class, why do you have to mark it? I hear Java is a safe language, is this, that makes it safe? Please clear me up. :)
    For final variables the compiler can optimize the code even further; for that private versus public thing: imagine more than one developer working on the same code; if you don't want something/somebody else using a method or touching a variable, make it private.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Webstorms is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Explanation of some Java certain aspects

    Thanks for the reply. So does the "final" keyword, make my program faster, smaller or both? -- How would the compiler do this, why and how would this differ from a variable without the keyword for the compiler? Say I don't want to share my code and keep it for myself, would there be any purpose of me still using private and public identifiers?

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Explanation of some Java certain aspects

    Quote Originally Posted by Webstorms View Post
    Thanks for the reply. So does the "final" keyword, make my program faster, smaller or both? -- How would the compiler do this, why and how would this differ from a variable without the keyword for the compiler? Say I don't want to share my code and keep it for myself, would there be any purpose of me still using private and public identifiers?
    The speed differences are miniscule, it's more to make your code organized and easy to understand.

    A private modifier could be useful if your project is open-source or you're working with others, like JosAH mentioned, and if it's just for you then it's for organization.

Similar Threads

  1. Need a java code and explanation for this
    By Crossover in forum New To Java
    Replies: 4
    Last Post: 09-21-2010, 02:23 AM
  2. Error Explanation? :)
    By Mirix in forum New To Java
    Replies: 6
    Last Post: 05-23-2010, 04:06 AM
  3. need explanation
    By marie in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 04:35 PM
  4. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 06:06 PM
  5. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-14-2007, 12:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •