Solved

static objects

Posted on 2004-03-21
10
244 Views
Last Modified: 2012-05-04
static key word when used with a class doesn't creates an object why?
0
Comment
Question by:meers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 3

Expert Comment

by:pamboo
ID: 10643536
can u be more clear with an example , please ?
0
 
LVL 2

Expert Comment

by:guntherothk
ID: 10645298
I assume you mean...

class Foo { /*foo's definition here*/ };

static Foo myFoo;

Sure it creases an object. It just doesn't call the memory allocator to construct the object on the heap. It creates a global variable. When you create a static variable, its visibility is limited. If you declare it inside a function, the variable is only visible inside the function. If you declare it outside any function, the variable is visible only within the current file.

If you said

static Foo* pFoo;

this creates an object too, but the object is of type "pointer to class Foo instance" and you have to assign an instance to the pointer. You can say

pFoo = &myFoo; // assign a static Foo instance to the pointer
pFoo = new Foo; // assign a dynamically created Foo instance to the pointer



0
 
LVL 30

Accepted Solution

by:
Mayank S earned 100 total points
ID: 10647909
In C++ and Java, a static class-member is like a shared member - meaning that, all objects will share the same value for that data-member. Like:

public class BankAccount // in Java
{
  private int iAccountNo ;
  private String sHolderName ;
  public static final double dRateOfInterest = 5.0 ;

  public BankAccount ( int iAccountNo, String sHolderName )
  {
    this.iAccountNo = iAccountNo ;
    this.sHolderName = sHolderName ;

  }

}

Here, any BankAccount objects, like:

BankAccount obj1 = new BankAccount ( 5, "meers" ) ;
BankAccount obj2 = new BankAccount ( 6, "mayankeagle" ) ;

- will have their own (different) values for iAccountNo and sHolderName, but they will have the same value for the static member dRateOfInterest. Also, the memory for dRateOfInterest is allocated before the allocation for obj1 and obj2, and it even statys after obj1 and obj2 are garbage-collected or de-allocated. And it is allocated only once - the same copy is used by all objects. You can access it directly using the class-name, as:

BankAccount.dRateOfInterest

- instead of obj1.dRateOfInterest.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 10647914
>> static key word when used with a class doesn't creates an object

Or do you mean about 'static' classes in Java? Only inner classes can be static.
0
 

Author Comment

by:meers
ID: 10650500
well i was talkin abt java static keyword mayankeagle
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10655995
So what more do you want to know? Have already told you about static in Java.
0
 

Expert Comment

by:trfishermi
ID: 10688551
Static objects are created at first access.  There can only be one copy of any static object in existance.  Once the static object is created, future attempts to create the ojbect will not actually "create" the object, instead they will simply reference the already created static object.

Tim
0
 

Author Comment

by:meers
ID: 10690150
okey thank you for your inputs for the static one.....i appreciate ur responses...now there is one for C++ write a class in c++ which cannot be inherited? ....meers
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10694464
Hi meers,

I would encourage you to ask fresh questions in a fresh question-page. Generally, it is bad practice to ask new questions on the same page and stretch it beyond its initial scope. The moderators also don't like it if they see it.

So it'll be better if could close this question and ask that one in a new page. But as such, I can still give you a single-line answer for that question: its not possible in C++, as far as I know, because you cannot have a 'final' class in C++ (you can have it in Java).

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10701216
Why a B :-(

A 'B' is not given if the question is easy. Have a look at:

http://www.experts-exchange.com/help/closing.jsp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn different types of Android Layout and some basics of an Android App.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question