Solved

static objects

Posted on 2004-03-21
10
233 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

803 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