Member variables vs. State Varaibels? Are they the same? [closed]

I am learning that member variables are to hold the state of an object? Is it their recommended usage? Are all member variables state variables too? or state variables have specific definition and are defined according to a class? In general what is the definition of an state variable?

In fact, I look for the best usage of member variables and how to avoid declaring unnecessary variables and using alternative approaches for example passing parameters to the methods of a class. As I think too many state variables may bring complexity to the state of class.

It’s really hard to give good advice on such a broad topic. The honest answer is: it depends on what you want to accomplish.

1) Member variables could represent state of an object. This might or might not be, what you intended to do. If your object lives in a web- / concurrency-context, then that is most of the time not, what you want. In these contexts, most of the time you want statelessness / immutability. If an object is shared and reads and writes are permitted, there is trouble. Of course there are ways to avoid such problems (e.g. locks etc.) but it is not a good idea to have mutable objects.

2) Another usecase for member variables is to hold dependencies for the object: e.g. dependencies to repositories, services, etc. These are most of the time injected once; sometimes via constructor-injection or via setter-injection. It would make no sense to avoid using membervariables for these dependencies; on the other hand you would have to inject needed dependencies with each function call. This pollutes the method signature unnecessary and makes using your api complicated.

3) And there is the space in between. If an object is not shared and the membervariable does not represent a dependency you are free to do, what you like. It is wrong to think of them as global variables since they are not global – they are limited to the object. And one pillar of object oriented design is encapsulation or information hiding. It’s like dancing naked in your living room: as long as nobody sees it – who cares?

A member variable is any variable that is scoped to the class. That means that any code within the class can access the variable. How the variable is used or how it is accessed doesn’t matter, with respect to it’s member status. Member variables and class/object state are the same thing.

Example:

public class MyClass
{
   // Not a member variable; never changes.
   private const pi = 3.14159;

   // class state; member variable, but shared with all object instances. 
   private static object locker;

   // member variable.
   private List<double> list;

   // member variable.
   private int counter;

   // member variable, but properties are preferred.
   public bool IsSingleton; 

   // not a member variable; part of the class's external API.
   public int Counter
   {
       get { return counter; }
   }

   public double CalculateAverage
   {
       // not a member variable; scoped to method.
       double sum;

       for (int i=0; i< list.Length; i++)
       {
           sum += list[i];
       }
       return sum / list.Length;
   }   
}

5

One use of “declaring variables as members” is for attributes. Them being things that describe the thing you are modeling that, maybe, some external consumer would want to ask. For example the name and age of a person are examples of attributes. These atributes are usually modified using a setter method and read using a getter method.

Attributes are part of the “state” of an object. But other variables besides the attributes are part of the state also.

The second possible use is for those kinds of variables. Such variables contain information that are usually read from and written to by several of the class method.

Examples:

  • an internal counter ( not the counters in for loops )
  • an open database connection
  • a list of processed items
  • a flag indicating the object is in certain state (waiting, closed, open, available, etc.)

These variables are candidates to be member variables even when no one outside the class need to know them.

What you say about just passing everything as parameters to the methods indicates that such methods are not cohesive to the class and that they should be static.

Maybe you are not modelling real world concepts in a OO way and you are just using classes as applications, i.e, the application is the class, and that’s why you don’t find it natural to declare member variables.

One additional tip is that if you find you are constantly passing the same parameter between several methods, chances are that method should be a member.

Recomendation:

Use classes to model real life entities, like BankAccount, Player, Chessman, etc. Don’t make a giant class with a giant main method and a lot of helper functions.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật