?
Solved

static objects

Posted on 2004-03-21
10
Medium Priority
?
253 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 300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes

765 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