Solved

static objects

Posted on 2004-03-21
10
224 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 115
java  and programming certification ? 4 86
Export Table to CSV - Access to CSV - using python 18 100
"Black Box" Testing of Control System Software 2 26
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about a problem I had getting the GPS LocationListener working.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

24 Experts available now in Live!

Get 1:1 Help Now