0% found this document useful (0 votes)
33 views4 pages

Assignment of AOOP

This document discusses the differences between local, instance, and static variables in Java. Local variables are declared within a method and can only be accessed within that method. Instance variables are declared within a class and can be accessed by any method in the class. Static variables are declared with the static keyword and are shared among all instances of a class. The scopes of local and instance variables are limited to the method or class they are declared in, while static variables can be accessed from outside the class using the class name.

Uploaded by

abdalle874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Assignment of AOOP

This document discusses the differences between local, instance, and static variables in Java. Local variables are declared within a method and can only be accessed within that method. Instance variables are declared within a class and can be accessed by any method in the class. Static variables are declared with the static keyword and are shared among all instances of a class. The scopes of local and instance variables are limited to the method or class they are declared in, while static variables can be accessed from outside the class using the class name.

Uploaded by

abdalle874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Abdullahi Abdi Ibrahim

Reg No: 22/1721/BIT-S

1. Difference between a local variable and an instance variable in java?


- Local variable: A local variable is declared within a method or block of code and is
accessible only within that method or block.

Example of local variable:


Public class assignment {
Public void exmplemethod(){
Int localvar=5; //local variable
//localvar is only accessible inside this method.
}
}

- Instance variable: an instance variable is declared within a class but outside of any
method, it belongs to the instance (object) of the class and can be accessed throughout
the class.

Example of Instance variable:

Public class assignment {


Int instancevar; // instance variable .
// instance variable have default value, but local variable can`t have default value.
}

2. Difference between STATIC variable and INSTANCE variable?


 STATIC variable: A static variable is shared among all instances ( objects) of a class its
declared with the `static` keyword and exist at the class level.

Example of static variable:


Public class assignment{
Static int staticvar=19; //static variable.
}

 Instance variable: an instance variable is declared within a class but outside of any
method, it belongs to the instance (object) of the class and can be accessed throughout
the class.
Example of Instance variable:

Public class assignment {


Int instancevar; // instance variable .
}

3. Declaring a local variable: // we declaring a local variable within a method or block by


specifying its type and name:

Example:

Public class assignment {


Public void exmplemethod(){
Int localvar; // declaration of a local variable
}
}

4. Declaring an instance variable: // we declaring an instance variable within a class


outside of any method.

Example of Declaration of an instance variable.


Public class assignment {
Int instancevar; // declaration of an instance variable.
}

5. Declaring a static variable: // we declare a static variable with the `static` keyword
typically within a class

Example of Declaration of a static variable.


Public class assignment{
Static int staticvar; // declaration of a static variable.

6. Scope of a local variable: // the scope of a local variable is limited to the method or
block in which it is declared.

Example of scope local variable:


Public class assignment {
Public void exmplemethod(){
Int localvar=10; // localvar`s scope is within this method.
}
}
7. Scope of an instance variable: // the scope of an instance variable is the entire class
where it is declared, making it accessible in all methods within the class.

Example of scope of an instance variable:


Public class assignment {
Int instancevar; // instance`s scope is entire class.
}

8. Scope of a static variable: // a static variable is just like an instance variable.

Example of scope of a static variable:


Public class assignment {
Static int staticvar; // the scope of static variable is entire class
}

9. Accessing a local variable outside its Scope:

A local variable can`t be accessed outside their scope so there`s no direct way to access
them outside the method or block where they are declared.

10. Accessing a static variable outside its Scope:

Example of Accessing a static variable outside its scope in java:

public class myExample {

static int staticvar=20; // static variable

public static void main(String[] args) {

int b=myExample.staticvar; //Accessing staticvar using class name

System.out.println(b);

You might also like