Java Variable Types and Rules for Declaring Variables

Variable is nothing it is only the name of the memory area.

While announcing factors we should observe rules given beneath.

Java Variable Types and Rules for Declaring Variables

Rules for Declaring Variables in Java

  • Variable name must bound with information type. It implies while
  • proclaiming a variable we should indicate its information type.
  • Ex: int x=10;
  • Save word or catchphrases can’t be taken as a variable name.
  • C
  • bolsters 32 character long factor names while in Java the length of a variable name can be limitless.
  • Nothing with the exception of 0-9, A-Z, a-z, _, $ can be utilized in factor
  • name.
  • Variable name must not begin with numeric and extraordinary
  • characters are additionally not permitted.

Example:

intabc=7; (valid)
int _abc=8; (valid)
int abc8=9; (valid)
int 8abc=10 (invalid)
int $_abc=12; (valid)
int $_abc*=15; (invalid)
6. Variable name should be meaningful.
Example:
string i=”Raj”; 
//valid but not meaningful
string name=”Raj”; //valid and meaningful

Note: Local factors must be introduced before the first use.

class demo
{
                public
static void main(String…s)
                {
                                int
x;      //must be initialized
                                System.out.println(x);
                }
}

Java Variable Types

There are three sorts of factors in Java that rare

an occurrence, neighbourhood and static.

Occurrence Variables

Occurrence factors are pronounced in a class, however outside any

strategy, constructor or square.

Nearby Variables

Nearby factors are pronounced in techniques, constructors or squares.

It must be introduced before the first use.

Static Variables

Static factors are proclaimed with the static catchphrase in a class, however outside any technique, constructor or square.

class A
{
                int
x=12;               //instance variable
                staticint
y=13;    //static variable
               
                public
static void main(String…s)
                {
                                int
z=30;               //local variable
                                System.out.println(x);
                                System.out.println(y);
                                System.out.println(z);
                }
}

Leave a Comment

error: Alert: Content is protected!!