We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

static objects

meers
meers asked
on
Medium Priority
276 Views
Last Modified: 2012-05-04
static key word when used with a class doesn't creates an object why?
Comment
Watch Question

Commented:
can u be more clear with an example , please ?
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



Principal Technologist
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.

Author

Commented:
well i was talkin abt java static keyword mayankeagle
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
So what more do you want to know? Have already told you about static in Java.
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

Author

Commented:
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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Why a B :-(

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

https://www.experts-exchange.com/help/closing.jsp
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.