Computer

Variables in Java

Variables in Java
Want create site? With Free visual composer you can do it easy.
Today let’s talk about variables in Java. In Java mainly we can have four different variable types.
1. Instance Variables (Non-Static Fields)
Instance variables are used to refer the objects(instances) of classes.2. Class Variables (Static Fields)
Here we use static modifier in front of the variable name and this tells the compiler that there is               exactly one copy of this variable in existence. If we use the keyword final also then that would                 indicate that the value of the variable will never change.​
3. Local Variables
These variables are declared inside a method and they are only visible within that particular                   method and not accessible from the rest of the class.4. Parameters
Let’s take the main method as an example, public static void main(String [] args){ } ,here args                  variable is the parameter to this method. And constructors also take parameter variables as                      arguments.You may be confused by the words ‘variable’ and ‘fields’. Local variables and Parameters are not called fields. An object stores its state in fields.

Java Variable Naming Rules

  • A variable name can begin with a letter, dollar sign ($) or underscore( _ ) . But it is better not to begin with a $ or _ because some auto-generated names start with a $ sign.
  • A variable name cannot begin with a digit.
  • White spaces are not allowed in a variable name.
  • We cannot use key words in Java as variable names.
abstract    continue    for                  new            switch
assert       default       goto               package    synchronized
boolean   do               if                     private       this
break        double       implements  protected  throw
byte           else            import            public       throws
case          enum         instanceof     return       transient
catch         extends     int                    short        try
char           final           interface        static         void
class          finally        long               strictfp       volatile

const*        float           native            super        while 

When you are declaring a non-static variable it is better to use Lower CamelCase Notation for it. Start the variable name with a lower case letter and then if it consists of more than one word, capitalize the first letter of each subsequent word. ex: thisIsLowerCamelCase 

Constant Variable Declaration

When declaring a constant variable, the standard is to use upper case for every letter, and if it consists of more than one word, separate them with underscore ( _ ) character.

ex: static final int START_POINT = 10 ;

Dinuka Jayaweera
Dinuka is a Contributor at Nerdynaut Staff from Sri Lanka.
Did you find apk for android? You can find new Free Android Games and apps.
  • Fascinated
  • Happy
  • Sad
  • Angry
  • Bored
  • Afraid

FB comments

Use 3dcart to create your online store.
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular

​Disclaimer : Nerdynaut is a tech company which consists of professionals in respective academic fields in its functional team facing challenging roles to quench the thirst of knowledge in cyberspace. Every article and majority of artworks in this website are copyright of Nerdynaut 2017. After a written permission from the publisher, the original content can be reused or referred for promotional work of education without any modification. Expressions of mass media with intentional harm on Nerdynaut brand may be liable to criminal prosecution and civil claims for the damages caused.
Protected by Copyscape
* = required field

Copyright©2014-2017.The Nerdynaut. All Rights Reserved. A game-changing subordinate of Naut's Holdings.
Made in Sri Lanka.

To Top