Solved

static objects

Posted on 2004-03-21
10
213 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:
mayankeagle 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
 
LVL 30

Expert Comment

by:mayankeagle
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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:mayankeagle
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
linearIn  challenge 23 65
seriesUp challenge 7 107
array6 challenfge 6 62
White board coding practice 3 61
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now