Stateful Objects are that objects which have changeable members or mutable members, that may vary what transactions or operations were previously performed on the object. For a same operation performed many times, the result outputted may be different from previous result's. It is very common to compare stateful objects with real world objects, where the state of the objects changes over-time. Syntax:
class classname
{
// declaring some states that are mutable
var state1
var state2
def changestate
{
// some operation to change the states of the object
}
}
When the members of an objects changes its value overtime for certain operation, which are performed on the object. Due to this the statefulness of the object changes that depends on the previously performed operations. Below are some examples to understand stateful object. Examples #1:
Scala
// A Scala program to illustrate
// stateful objects
// creating a class
class waterbottle
{
// creating states
var water: Int = 0
def drinkwater = {
if(water > 0)
{
water = water-1
println("water left = "+water)
}
else
{
println("waterbottle empty fill water")
}
}
// Defining method
def fillwater (c: Int)=
{
if(water + c > 5)
{
water = 5
}
else
{
water = water + c
}
}
override def toString= "water in bottle = " + water
}
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// waterbottle object
var w = new waterbottle
// calling
w.fillwater(3)
println(w)
// Changing state
w.drinkwater
w.drinkwater
w.drinkwater
w.drinkwater
}
}
Output :
water in bottle = 3
water left = 2
water left = 1
water left = 0
waterbottle empty fill water
As we can see in the above example state for w.drinkwater changes its result for the same operation to "waterbottle empty fill water" till we fill or update water. The mutable state depends on water which is a variable. Thus we can say that stateful objects are made from vars and not val but this is not true, a class can change its state without containing any vars. For an object that changes its state to another state, the set of operation or the path taken to reach that particular state can be different, but at the end what state is achieved should be same. This is also known as operational equivalence where x & y are different objects but with the same states at the end of different set of operations. Examples #2:
Scala
// A Scala program to illustrate
// stateful objects
// creating player class
class player
{
// creating states
var health: Int = 10
def punch(p: player)
{
if(p.health > 0)
{
p.health = p.health-2
println(p + " health is " + p.health)
if(p.health < 1)
{
// checking the state
dead(p)
}
}
else
{
dead(p)
}
}
def kick(p: player)
{
if(p.health > 0)
{
p.health = p.health-3
println(p + " health is " + p.health)
if(p.health < 1)
{
// checking the state
dead(p)
}
}
else
{
dead(p)
}
}
def superp(p: player)
{
if(p.health > 0)
{
p.health = p.health - 5
println(p + " health is " + p.health)
if(p.health < 1)
{
// checking the state
dead(p)
}
}
else
{
dead(p)
}
}
def dead(p:player)
{
println("Game Over")
println(p +" is dead " + this + " is winner")
}
}
// Creating object
object GFG
{
// Main method
def main(args: Array[String])
{
// Creating objects for player
var p1 = new player
var p2 = new player
p1.kick(p2)
p1.punch(p2)
p1.superp(p2)
p1.punch(p2)
}
}
Output :
player@506e1b77 health is 7
player@506e1b77 health is 5
player@506e1b77 health is 0
Game Over
player@506e1b77 is dead player@4fca772d is winner
Game Over
player@506e1b77 is dead player@4fca772d is winner
Similarly in the above example we can tell that player is a stateful object without looking at the internal working of the class. Because the health cannot be reduced to negative so player has mutable states as the same operation returns different output at different or same inputs.
Similar Reads
Stack in Scala A stack is a data structure that follows the last-in, first-out(LIFO) principle. We can add or remove element only from one end called top. Scala has both mutable and immutable versions of a stack. Syntax : import scala.collection.mutable.Stack var s = Stack[type]() // OR var s = Stack(val1, val2, v
3 min read
Scala Stream The Stream is a lazy lists where elements are evaluated only when they are needed. This is a scala feature. Scala supports lazy computation. It increases performance of our program. Streams have the same performance characteristics as lists. Syntax : val str = 1 #:: 2 #:: 3 #:: Stream.empty In scala
3 min read
Scala | Traits Introduction to Traits in Scala:In Scala, Traits are a fundamental concept of object-oriented programming that provides a mechanism to reuse code. Traits are similar to interfaces in Java, but with the added advantage of providing concrete implementations of methods as well as the ability to include
7 min read
Scala Type Hierarchy There are no primitive types in Scala(unlike Java). All data types in Scala are objects that have methods to operate on their data. All of Scala's types exist as part of a type hierarchy. Every class that we define in Scala will also belong to this hierarchy automatically. AnyAny is the superclass o
3 min read
Scala AnyRef type The Scala kind hierarchy starts with Any, that's the supertype of all types. The direct subclasses of Any are AnyVal and AnyRef. Whereas AnyVal represents price kinds (which includes Int, Double, and so on.), AnyRef represents reference types. AnyRef serves because the fundamental type and root deta
3 min read
Getters and Setters in Scala Getter and Setter in Scala are methods that helps us to get the value of variables and instantiate variables of class/trait respectively. Scala generates a class for the JVM with a private variable field and getter and setter methods. In Scala, the getters and setters are not named getXxx and setXxx
4 min read