Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Instance variable
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Examples== === C++ === <syntaxhighlight lang="cpp"> struct Request { static int count1; // variable name is not important int number; Request() { number = count1; // modifies the instance variable "this->number" ++count1; // modifies the class variable "Request::count1" } }; int Request::count1 = 0; </syntaxhighlight> In this [[C++]] example, the instance variable <code>Request::number</code> is a copy of the [[class variable]] <code>Request::count1</code> where each instance constructed is assigned a sequential value of <code>count1</code> before it is [[increment operator|incremented]]. Since <code>number</code> is an instance variable, each <code>Request</code> object contains its own distinct value; in contrast, there is only one object <code>Request::count1</code> available to all class instances with the same value. === Java === <syntaxhighlight lang="java"> //Example.java class Example { public int x = 0; public void setX(int newValue) { this.x = newValue; } } //Main.java class Main { public static void main(String[] args) { Example example1 = new Example(); Example example2 = new Example(); //We can set the value of x by itself, as the variable is public example1.x = 10; assert example1.x == 10; assert example2.x == 0; //As setX is an instance method, it can also access the variable example2.setX(-10); assert example1.x == 10; assert example2.x == -10; } } </syntaxhighlight> In this [[Java (programming language)|Java]] example, we can see how instance variables can be modified in one instance without affecting another. === Python === <syntaxhighlight lang="python"> class Dog: def __init__(self, breed): self.breed = breed # instance variable # dog_1 is an object # which is also an instance of the Dog class dog_1 = Dog("Border Collie") </syntaxhighlight>In the above [[Python (programming language)|Python]] code, the instance variable is created when an argument is parsed into the instance, with the specification of the breed positional argument.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Instance variable
(section)
Add topic