Instance Variables and Class Variables
Non-Static and Static Fields in a Class
Instance (Non-Static) variables are
unique to each instance (object) of a class. It represents the specific
attributes of the objects. Class (Static) variables are shared among all instances of a
class. It represents properties or values common to the entire class.
Instance Variables:
- In object-oriented language, it is known as "member variables" or "instance fields."
- Instance or an Object of a class (i.e., each object created from the class) has its own separate copy of fields or instance variables.
- An instance variable represents the attributes or properties of objects created from the class (i.e. information of the required of the Entity).
- Instance variables are typically declared within the class but outside of any methods.
- Instance variables are accessed using object references.
Class Variables:
- It is also known as "Static Variables" or "Class Fields."
- Class variables are shared among all instances (objects) of a class.
- It is only one copy of a class variable that is shared across all instances of the class.
- It is typically used for values or properties that are common to all instances of the class.
- Class variables are declared using the static or shared keyword within the class.
Scenario:
In object-oriented
programming (OOP), an "Entity" is a very broad term. It generally
refers to a real-world object, concept, or thing. It is modelled as an class in the software. In OOP, entities are modelled as a class. The objects of the
class represent instances of this entity.
A real-world entity say Student. To model or use the Entity Student in a software, It is defined as a class ( user-defined data type). The information required of the Student are Roll-Number, Name and branch of Engineering (assume Computer Engineering) in which admission taken. The information required of an Entity becomes data-members or fields of the class, The Student class in Java can be defined as follows:
class Student {
//Instance fields or Non-Static fields
//seperate copy or memory is allocated when object or instance of class is created
private int rollNo;
private String name;
private String branch;
}
class Student {
//Instance fields or Non-Static fields
//separate copy or memory is allocated when object or instance of class is created
private int rollNo;
private String name;
//Class Variable or Static field
//Only a Single copy of Memory is allocated
private static String branch;
}
class Test {
private:
int a;
public:
int b;
};
int main() {
//Object created of Test class
Test test;
//test.a = 10; Not accessible as it is private
test.b = 20; //Accessible as it is public
return 0;
}
Getters and Setters in OOPS:
- Getter: A method that allows to access or get or retrieve an attribute in a given class.
- Setter: A method that allows to set or mutate or assign the value of an attribute in a class.
class Test {
private:
int a;
public:
int b;
//Getter
int getA() {
return a;
}
//Setter
void setA(int a) {
this->a = a;
}
};
int main() {
//Object created of Test class
Test test;
//test.a = 10; Not accessible as it is private
test.b = 20; //Accessible as it is public
test.setA(10);
std::cout<<"a = "<<a;
return 0;
}
Instance Variables and Class Variables in C++:
using namespace std;
class Student{
private:
int rollNo;
string name;
static string branch;
public:
Student(){
}
//Getters
int getRollNo(){
return this->rollNo;
}
//Setters
void setRollNo(int rollNo){
this->rollNo = rollNo;
}
string getName(){
return this->name;
}
void setName(string name){
this->name = name;
}
static string getBranch(){
return branch;
}
static void setBranch(string Vbranch){
branch = Vbranch;
}
};
string Student::branch="";
int main(){
//Create Objects
Student s1;
s1.setRollNo(101);
s1.setName("Joe");
Student::setBranch("Computer Engineering");
cout<<"Branch: "<<student::getbranch();
std::cout<<"Roll No. : "<<s1.getRollNo()<<"Name : "<<s1.getName()<<endl;
Student s2;
s2.setRollNo(102);
s2.setName("James");
std::cout<<"Roll No. : "<<s2.getRollNo()<<"Name : "<<s2.getName()<<endl;
}
Instance Variables and Class Variables in Java:
public class Student {
private int rollNo;
private String name;
private static String branch;
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getBranch() {
return branch;
}
public static void setBranch(String branch) {
Student.branch = branch;
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student();
s1.setRollNo(110);
s1.setName("Joe");
Student.setBranch("Computer Engineering");
System.out.println("Branch :" + Student.getBranch());
System.out.println("Roll No. :" + s1.getRollNo() + " Name : " + s1.getName());
Student s2 = new Student();
s2.setRollNo(111);
s2.setName("James");
System.out.println("Roll No. :" + s2.getRollNo() + " Name : " + s2.getName());
}
}
Instance Variables and Class Variables in C#.NET:
using System;
namespace Test
{
class Student
{
public int RollNo {get; set; }
public string Name{get; set;}
public static string Branch{get; set;}
}
public class Program
{
public static void Main(string[] args)
{
Student s1 = new Student();
s1.RollNo =101;
s1.Name ="Joe";
Student.Branch = "Computer Engineering";
Console.WriteLine("Branch :" + Student.Branch);
Console.WriteLine("Roll No. " + s1.RollNo + " Name :" + s1.Name);
Student s2 = new Student();
s2.RollNo =102;
s2.Name ="James";
Console.WriteLine("Roll No. " + s2.RollNo + " Name :" + s2.Name);
}
}
}
Instance Variables and Class Variables in VB.NET:
Public Class Student
'Instance Fields or Variable
Private rno As Integer
Private nm As String
'Class Field or variable
Private Shared br As String
Public Property RollNo() As Integer
Get
Return rno
End Get
Set(value As Integer)
rno = value
End Set
End Property
Public Property Name() As String
Get
Return nm
End Get
Set(value As String)
nm = value
End Set
End Property
Shared Property Branch() As String
Get
Return br
End Get
Set(value As String)
br = value
End Set
End Property
End Class
Module Module1
Sub Main()
Dim s1 As New Student
s1.RollNo = 101
s1.Name = "Joe"
Student.Branch = "Computer Engineering"
Console.WriteLine("Branch :" + Student.Branch)
Console.WriteLine("Roll No. : " & s1.RollNo & " Name : " & s1.Name)
Dim s2 As New Student
s2.RollNo = 102
s2.Name = "James"
Console.WriteLine("Roll No. : " & s2.RollNo & " Name : " & s2.Name)
Console.ReadKey()
End Sub
End Module
Instance Variables and Class Variables in Python:
class Person:
def __init__(self, name):
self._name = name # Private attribute with an underscore
@property
def name(self):
return self._name
@name.setter
def name(self, new_name):
if new_name != "":
self._name = new_name
class Student:
#Class Variable
branch = ""
def __init__(self):
self._rollno = 0 # Private attribute with an underscore
self._name = ""
def __init__(self, rollno, name):
self._rollno = rollno # Private attribute with an underscore
self._name = name
@property
def rollno(self):
return self._rollno
@rollno.setter
def rollno(self, new_rno):
if new_rno != "":
self._rollno = new_rno
@property
def name(self):
return self._name
@name.setter
def name(self, new_name):
if new_name != "":
self._name = new_name
Student.branch = "Computer Engineering"
# Creating a Person object
student = Student(0, "")
# Modifying the attribute using the setter method
student.rollno = 101
student.name = "Joe"
print("Branch :" ,Student.branch)
print("Roll No. " ,student.rollno, " Name :", student.name)
student.rollno = 102
student.name = "James"
print("Roll No. " ,student.rollno, " Name :", student.name)
Instance Variables and Class Variables in PHP:
In PHP, One can create
getter and setter methods for class fields or properties to encapsulate access to
the declared properties. The methods allow controlling how properties are
accessed and modified and thereby maintaining data encapsulation.
A Getter method is
defined as a method t0 retrieve the value of a property. The naming convention for
getter method is like getPropertyName, where PropertyName is the name of the property
to be accessed. Similarly for Setter , the name convention is setPropertyName.
<?php
class Person {
private $rollNo;
private $name;
public static $branch; // Static property
public function setRollNo($newRollNo) {
$this->rollNo = $newRollNo;
}
public function getRollNo() {
return $this->rollNo;
}
public function setName($newName) {
if ($newName != "") {
$this->name = $newName;
}
}
public function getName() {
return $this->name;
}
}
Person::$branch = "Computer Engineering";
// Creating a Person object
$person = new Person();
$person->setRollNo(101);
$person->setName("Joe");
echo "Branch :".Person::$branch;
echo "\nRoll No. :".$person->getRollNo();
echo " Name :" .$person->getName();
$person->setRollNo(102);
$person->setName("James");
echo "\nRoll No. :".$person->getRollNo();
echo " Name :" .$person->getName();
?>
easy to understand
ReplyDelete