## Definition
A [[variable]] associated with either a [[class]] or an [[object]], whose value exists as part of the class's or object's [[object state|state]].
*Noun.*
> The `BankAccount` class has a `__balance` attribute that stores the account's current funds.
## Characteristics
- Used in [[object-oriented programming]];
- can be either [[public]] or [[private]], controlling access from outside the class;
- can exist at either the class or instance level:
- [[#Class-level attribute|class-level attributes]] are shared across all [[instance|instances]] of the class; and
- [[#Instance-level attribute|instance-level attributes]] are unique to each object;
- can be [[initialise|initialised]] with [[argument|arguments]] passed to the [[constructor]] or with default values assigned in the constructor;
- could be accessed using a [[getter]]; and
- could be changed using a [[setter]].
## Examples
### Instance-level attribute
In Python, instance-level attributes are declared and initialised in the constructor. Such attributes are unique to each object, which means changing the value of an instance-level attribute in one object does not affect other objects of the same class. For example, changing the `__balance` attribute of the `james` object will not affect the `__balance` attribute of the `jeremy` object.
```python {4-6}
class BankAccount:
def __init__(self, p_owner, p_initial_balance):
self.owner = p_owner # instance-level attribute
self.__balance = p_initial_balance # instance-level attribute
self.account_type = "Savings" # instance-level attribute
```
### Class-level attribute
Class-level attributes are defined on the class itself rather than on individual [[object|objects]]. They are shared across all [[instance|instances]] of a class in shared [[object state|state]]. Any changes to such attributes affect all instances of the class.
In this `BankAccount` class, every instance can access `interest_rate`; this class-level attribute has the same value for all instances.
```python
class BankAccount:
interest_rate = 0.03 # class-level attribute
def __init__(self, p_owner):
self.owner = p_owner
```
### Modifying a public attribute
The values of [[public]] attributes can be changed by accessing them directly outside the class definition.
```python
class BankAccount:
...
b = BankAccount("Timmy Banks", 500)
b.owner = "Timmy Gold" # direct access to the owner attribute of the b object
```
### Modifying an attribute with a method
The values of attributes can be changed using [[method|methods]], including by [[setter|setters]].
```python
class BankAccount:
...
def set_name(self, p_name):
self.name = p_name # modify the name attribute in a setter
def deposit(self, p_amount):
if p_amount > 0:
self.__balance = self.__balance + p_amount # modify the __balance attribute
b = BankAccount("Timmy Banks", 500)
b.deposit(50)
```
### Declaring an attribute (Java)
Many programming languages, including Java, [[declare]] attributes at the start of the class definition rather than in the [[constructor]].
```java
public class BankAccount {
// attributes
public String name;
private double balance;
public String accountType = "Savings"; // attribute with a default value
// constructor
public BankAccount(String pOwner, double pInitialBalance) {
this.owner = pOwner;
this.balance = pInitialBalance;
}
}
```
## Non-examples
### Parameter
A [[parameter]] is not an attribute.
```python
class BankAccount:
def __init__(self, p_owner, p_initial_balance):
self.owner = p_owner
self.__balance = p_initial_balance
self.account_type = "Savings"
```
`p_owner`, `p_initial_balance` are both parameters; `owner`, `__balance`, and `account_type` are all attributes.
### Variable
A [[variable]] is not an attribute.
```python
class Enemy:
...
def heal(self):
heal_amount = 10 # variable
self.health = self.health + heal_amount # modify an attribute
```
`heal_amount` is a variable and not an attribute; `health` is an attribute.
In Python, attributes are accessed from within a class definition with the `self.` prefix.
### Class
A [[class]] defines the template used to create an [[object]], including which attributes the object will have.
### Value
An attribute has a value but is not itself a value (much like a [[variable]]).
### Method
[[method|Methods]] define the behaviour of a class.