• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variables hold the state of the program and methods operate on that state  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
variables like int,string etc hold values which change and we can also have types of those fields aka variables which can be given access modifiers and some types of variables have default values as well. But i am still failing to understand what exactly does the statement mean. Encountered this statement while going through JAVA SE 8 OCA I - 808.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a link to the specific text you are referring to?

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Carey,

The text i am referring to is on kindle edition of JAVA SE 8 OCAJP-I book in the first chapter. I have do not have any soft copy of that...
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:variables like int,string etc hold values which change and we can also have types of those fields aka variables which can be given access modifiers and some types of variables have default values as well. But i am still failing to understand what exactly does the statement mean.

Me too, because I doubt that it actually said what you wrote.

First of all, "int, string [which should actually be String] etc" are NOT variables, they're types.

Secondly, String values don't change, because String is immutable.

Now I don't want to swamp you with terminology here, but it is really important that you understand the ones that the book is using.

In the declaration: "private String firstName;":
  • 'String' is the type.
  • 'firstName' is the variable.           and
  • 'private' is the access modifier.
  • and 'firstName' is a variable not because you can change the contents of a String, but because you can change IT (ie, firstName) to hold a different String, viz:
      private String firstName = "a new first name";
      ...
      firstName = "a different first name";
    but if the declaration was:
      private final String firstName;
    you couldn't; and 'firstName' would then be a constant.

    Now, getting back to your question: What exactly is it about the statement that you don't understand?

    My advice would be to copy the statement (or paragraph) that's causing you problems, and and paste it here exactly as it appears in the book, and then ask your question again - maybe with a bit more detail about what it is you don't understand.

    HIH

    Winston
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi

    Winston,

    This is what is written in the book...

    Java classes have two primary elements: methods, often called functions or procedures in other languages, and fields, or more generally known as variables. Together these are called members of the class. Variables hold the state of the program, and members operate on that state. If the change is important to remember, a variable stores that change.

    The last sentence is what i did not get properly..

    Does the STATE here means the values that the variables store... ?
     
    Campbell Ritchie
    Marshal
    Posts: 55768
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The state of an object means all the values of its fields combined. So if one field changes the state changes. I think you have misread what the book says; it probably says methods operate on that state. It is a bit inaccurate to say that fields are called variables; there are some variables which are not fields and some fields may be constant so they are not variables.

    Note that static fields do not actually form part of the object.
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    krishnadhar Mellacheruvu wrote:Does the STATE here means the values that the variables store... ?

    Basically, yes. But as Campbell says, it usually refers to the current values of ALL variables that a class has collectively.

    For example, the "state" of a ChessGame class would probably be the positions of all pieces currently on its 'board'.

    HIH

    Winston
     
    krishnadhar Mellacheruvu
    Ranch Hand
    Posts: 118
    Android Java Objective C
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi

    Ricthie,

    Yup, thats a type mistake from my side, it is indeed methods and not members,

    @Winston

    Thats an awesome example to make it understandable....
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    krishnadhar Mellacheruvu wrote:Thats an awesome example to make it understandable....

    Glad you liked it. And welcome to JavaRanch.

    Winston
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!