Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > BOOK: Beginning Object-Oriented Programming with C#
|
BOOK: Beginning Object-Oriented Programming with C#
This is the forum to discuss the Wrox book Beginning Object-Oriented Programming with C# by Jack Purdum; ISBN: 978-1-1183-3692-2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Object-Oriented Programming with C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 27th, 2013, 04:18 AM
Authorized User
 
Join Date: Aug 2012
Posts: 19
Thanks: 8
Thanked 0 Times in 0 Posts
Default Class private member and class static member difference ?

Hi .
I could not understand difference between private and static member in classes.
The code within class has direct access to objects in both of them.
So the difference is : when you use static member, you don't need to instantiate an object. is it right ?

Thank u.
 
Old June 29th, 2013, 02:38 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default static versus private.

Suppose you have a class named clsPerson with a private member named Age. if you write code that creates 100 clsPerson objects, you will get 100 private members named Age.

Now suppose that class has a variable defined as:

static String days[]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};

Because you used the keyword static the compiler does two important things:

1) Immediately at program start, it creates the days[] array, even if you have not defined a clsPerson yet.
2) It will never create another days[] array regardless of how many other new clsPerson objects you define.

This means that every clsPerson object shares the same days[] array...it is never reassigned. Therefore, anytime you have data that likely won't change during program execution, making it a static can save code space by not duplicating the data. It should also be obvious that static data within methods retains its values between calls to the method.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
The Following User Says Thank You to DrPurdum For This Useful Post:
kafshdoozak (June 30th, 2013)
 
Old June 30th, 2013, 02:05 AM
Authorized User
 
Join Date: Aug 2012
Posts: 19
Thanks: 8
Thanked 0 Times in 0 Posts
Default static and private

Hi.
Thank you so much .
Before your explanation i hadn't got the differences .





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch7 Const Member Functions of a Class proslambano BOOK: Ivor Horton's Beginning Visual C++ 2005 1 March 5th, 2007 05:01 PM
Arraylist::Sort() by class member jilly Visual C++ 2005 0 March 22nd, 2006 06:49 PM
How to Assign a Desc. to a Class Member BrianWren VB.NET 2002/2003 Basics 3 June 1st, 2005 02:07 PM
Invoking event from member variable class adxabuan Visual C++ 0 February 2nd, 2004 06:04 AM
Forum class (member) explination?? drfunkie BOOK: ASP.NET Website Programming Problem-Design-Solution 0 June 15th, 2003 07:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.