How does java store variables




















Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use, and the Java programming language is no different.

The rules and conventions for naming your variables can be summarized as follows:. All rights reserved. Hide TOC. Create a variable called myNum of type int and assign it the value 15 :.

Note that if you assign a new value to an existing variable, it will overwrite the previous value:. Change the value of myNum from 15 to 20 :. However, you can add the final keyword if you don't want others or yourself to overwrite existing values this will declare the variable as "final" or "constant", which means unchangeable and read-only :.

All Java variables must be identified with unique names. Note: It is recommended to use descriptive names in order to create understandable and maintainable code:. What we now have between the round brackets of println is some direct text enclosed in double quotes:. The plus sign means "join together". So we're joining together the direct text and the name of our variable.

The joining together is known as concatenation. Your coding window should now look like this note how each line of code ends with a semicolon :. Run your programme and you should see the following in the Output window at the bottom:. Let's try some simple addition. Add two more int variables to your code, one to store a second number, and one to store an answer:. Notice how we have three variable names on the same line.

Since their lifetimes span the length of the method in which they are declared, local variables compare to a notepad that appears every time you receive a telephone call, but disappears when you hang up. That setup can be useful for jotting down notes, but sometimes you need something more permanent. What's a programmer to do? Enter member variables. Member variables -- of which there are two, instance and static -- make up part of a class.

Developers implement instance variables to contain data useful to a class. An instance variable differs from a local variable in the nature of its scope and its lifetime. The entire class makes up the scope of an instance variable, not the method in which it was declared.

In other words, developers can access instance variables anywhere in the class. In addition, the lifetime of an instance variable does not depend on any particular method of the class; that is, its lifetime is the lifetime of the instance that contains it. Instances are the actual objects that you create from the blueprint you design in the class definition. You declare instance variables in the class definition, affecting each instance you create from the blueprint.

Each instance contains those instance variables, and data held within the variables can vary from instance to instance. Consider the AlarmClock class. Passing the snoozeInterval into the snooze method isn't a great design.

Imagine having to type in a snooze interval on your alarm clock each time you fumbled for the snooze button. Instead, just give the whole alarm clock a snoozeInterval.

You complete this with an instance variable in the AlarmClock class, as shown below:. You can access instance variables almost anywhere within the class that declares them. To be technical about it, you declare the instance variable within the class scope , and you can retrieve it from almost anywhere within that scope. Practically speaking, you can access the variable anywhere between the first curly bracket that starts the class and the closing bracket.

Since you also declare methods within the class scope, they too can access the instance variables. You can also access instance variables from outside the class, as long as an instance exists, and you have a variable that references the instance.



0コメント

  • 1000 / 1000