473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Static Object

Hello to all of you,

I want to know that what's the use to create static object.

Thanks You
Pramod Sahgal

Jul 30 '07 #1
10 2620
Roy
On Jul 30, 11:40 am, Pramod <sahgalpram...@gmail.comwrote:
Hello to all of you,

I want to know that what's the use to create static object.

Thanks You
Pramod Sahgal
Hi ,

The use of creation of a static object can be to create a connection
object for a database .

Regrads,

Ashish.

Jul 30 '07 #2
On Jul 30, 11:51 am, Roy <royash...@gmail.comwrote:
On Jul 30, 11:40 am, Pramod <sahgalpram...@gmail.comwrote:
Hello to all of you,
I want to know that what's the use to create static object.
Thanks You
Pramod Sahgal

Hi ,

The use of creation of a static object can be to create a connection
object for a database .

Regrads,

Ashish.
hello Ashish
not clear what do you want to say will you explain it.

Jul 30 '07 #3
Pramod wrote:
I want to know that what's the use to create static object.
Basically a static object is a global variable which visibility
is restricted to be inside the thing where it was declared. For
example, if you declare a static variable inside a class, the
lifetime of that static variable will be like the lifetime of a
global variable, but the scope variable will be limited to be inside
the class. In other words:

class A
{
static int i;
};

....

int A::i = 5;

Here 'i' will behave like a global variable, ie. it will be
initialized to 5 before main() is executed, and it will be
destroyed after main() has ended. It also behaves like a "global"
variable in that it's not bound to any specific instance of A,
but all the instances will use the one and same variable, and in
fact you can access it without an instance of A. The only difference
with a global variable is that 'i' is inside the scope of A and
can only be accessed through A (and it will not cause name collisions
outside of A).

A static variable inside a function is similar: It behaves like it
was a global variable, except that its visibility will be limited to
be inside that function. There's one other major difference with
global variables, though: It will be initialized the first time the
function is called (instead of some time before main() is called).
In practice this means that a static variable inside a function
will preserve its value between function calls.
For example:

void foo()
{
static int i = 0;

++i;
std::cout << i << std::endl;
}

'i' will behave like it was a global variable: The "++i" will
increment its value, and it will preserve that new value even after
the function is exited. The next time the function is called 'i' will
have the value 1 (which is then incremented to 2), and so on.
The initialization to 0 will be performed only once (not each time
the function is called).

The small twist is, as already mentioned, that 'i' will be initialized
the first time foo() is called. That allows doing something like:

void foo(int value)
{
static int i = value;
...
}

The 'i' variable will be initialized to the value of the function
parameter only the first time the function is called. Subsequent calls
will not modify it. (Naturally if you want it to initialize it each
time, obviously don't use 'static'.)

Why are static variables useful? Static member variables of a class
are called "class members". It's sometimes useful to have variables
which are common to all instances of a class, but which scope is
limited to be inside that class (so as to not to garbage the global
scope).
In my experience static variables inside functions are less useful
(mainly it's a historical feature from C) because usually what you
would want to do with a static function variable can be better done
with a class, but I suppose some people have found them useful too.
Jul 30 '07 #4
On Jul 30, 3:00 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Pramod wrote:
I want to know that what's the use to create static object.

Basically a static object is a global variable which visibility
is restricted to be inside the thing where it was declared. For
example, if you declare a static variable inside a class, the
lifetime of that static variable will be like the lifetime of a
global variable, but the scope variable will be limited to be inside
the class. In other words:

class A
{
static int i;

};

...

int A::i = 5;

Here 'i' will behave like a global variable, ie. it will be
initialized to 5 before main() is executed, and it will be
destroyed after main() has ended. It also behaves like a "global"
variable in that it's not bound to any specific instance of A,
but all the instances will use the one and same variable, and in
fact you can access it without an instance of A. The only difference
with a global variable is that 'i' is inside the scope of A and
can only be accessed through A (and it will not cause name collisions
outside of A).

A static variable inside a function is similar: It behaves like it
was a global variable, except that its visibility will be limited to
be inside that function. There's one other major difference with
global variables, though: It will be initialized the first time the
function is called (instead of some time before main() is called).
In practice this means that a static variable inside a function
will preserve its value between function calls.
For example:

void foo()
{
static int i = 0;

++i;
std::cout << i << std::endl;

}

'i' will behave like it was a global variable: The "++i" will
increment its value, and it will preserve that new value even after
the function is exited. The next time the function is called 'i' will
have the value 1 (which is then incremented to 2), and so on.
The initialization to 0 will be performed only once (not each time
the function is called).

The small twist is, as already mentioned, that 'i' will be initialized
the first time foo() is called. That allows doing something like:

void foo(int value)
{
static int i = value;
...

}

The 'i' variable will be initialized to the value of the function
parameter only the first time the function is called. Subsequent calls
will not modify it. (Naturally if you want it to initialize it each
time, obviously don't use 'static'.)

Why are static variables useful? Static member variables of a class
are called "class members". It's sometimes useful to have variables
which are common to all instances of a class, but which scope is
limited to be inside that class (so as to not to garbage the global
scope).
In my experience static variables inside functions are less useful
(mainly it's a historical feature from C) because usually what you
would want to do with a static function variable can be better done
with a class, but I suppose some people have found them useful too.
Hello Juha Nieminen,
Thanks for give me reply...but my question is that Static object like

class Foo
{

Jul 30 '07 #5
On Jul 30, 3:00 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Pramod wrote:
I want to know that what's the use to create static object.

Basically a static object is a global variable which visibility
is restricted to be inside the thing where it was declared. For
example, if you declare a static variable inside a class, the
lifetime of that static variable will be like the lifetime of a
global variable, but the scope variable will be limited to be inside
the class. In other words:

class A
{
static int i;

};

...

int A::i = 5;

Here 'i' will behave like a global variable, ie. it will be
initialized to 5 before main() is executed, and it will be
destroyed after main() has ended. It also behaves like a "global"
variable in that it's not bound to any specific instance of A,
but all the instances will use the one and same variable, and in
fact you can access it without an instance of A. The only difference
with a global variable is that 'i' is inside the scope of A and
can only be accessed through A (and it will not cause name collisions
outside of A).

A static variable inside a function is similar: It behaves like it
was a global variable, except that its visibility will be limited to
be inside that function. There's one other major difference with
global variables, though: It will be initialized the first time the
function is called (instead of some time before main() is called).
In practice this means that a static variable inside a function
will preserve its value between function calls.
For example:

void foo()
{
static int i = 0;

++i;
std::cout << i << std::endl;

}

'i' will behave like it was a global variable: The "++i" will
increment its value, and it will preserve that new value even after
the function is exited. The next time the function is called 'i' will
have the value 1 (which is then incremented to 2), and so on.
The initialization to 0 will be performed only once (not each time
the function is called).

The small twist is, as already mentioned, that 'i' will be initialized
the first time foo() is called. That allows doing something like:

void foo(int value)
{
static int i = value;
...

}

The 'i' variable will be initialized to the value of the function
parameter only the first time the function is called. Subsequent calls
will not modify it. (Naturally if you want it to initialize it each
time, obviously don't use 'static'.)

Why are static variables useful? Static member variables of a class
are called "class members". It's sometimes useful to have variables
which are common to all instances of a class, but which scope is
limited to be inside that class (so as to not to garbage the global
scope).
In my experience static variables inside functions are less useful
(mainly it's a historical feature from C) because usually what you
would want to do with a static function variable can be better done
with a class, but I suppose some people have found them useful too.
Hello Juha ,

Thanks for give me answer but my question is Static Object

class Foo
{

...

};
int main()
{
static Foo *static_object;
.........
.........
return 0;
}

What's the use to create this type of object?

Jul 30 '07 #6
Pramod wrote:
int main()
{
static Foo *static_object;
I think I answered that question?
Jul 31 '07 #7
On Jul 31, 3:32 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Pramod wrote:
int main()
{
static Foo *static_object;

I think I answered that question?
will you explain me again about static object?? please

Aug 1 '07 #8
On Jul 30, 12:18 pm, Pramod <sahgalpram...@gmail.comwrote:
On Jul 30, 3:00 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Hello Juha ,

Thanks for give me answer but my question is Static Object

class Foo
{

...

};

int main()
{
static Foo *static_object;
.........
.........
return 0;

}

What's the use to create this type of object?
First of all, you don't create an object in strict sense, your create
a pointer to an object that doesn't exists yet.

For the rest it's useless,unless you call main yourself in your code,
which I hope you don't. Static object are used to share (like globals)
between different pieces of code. But for main goes that is executed
once
so your variable is shared between all (that is one) instances of
main.
So if you leave out the static word nothing[1] will change.

Good luck,
Bas

[1] the time your pointer is created will change, but you will not
notice this.
Aug 1 '07 #9
On Jul 30, 1:18 pm, Pramod <sahgalpram...@gmail.comwrote:
On Jul 30, 3:00 pm, Juha Nieminen <nos...@thanks.invalidwrote:


Pramod wrote:
I want to know that what's the use to create static object.
Basically a static object is a global variable which visibility
is restricted to be inside the thing where it was declared. For
example, if you declare a static variable inside a class, the
lifetime of that static variable will be like the lifetime of a
global variable, but the scope variable will be limited to be inside
the class. In other words:
class A
{
static int i;
};
...
int A::i = 5;
Here 'i' will behave like a global variable, ie. it will be
initialized to 5 before main() is executed, and it will be
destroyed after main() has ended. It also behaves like a "global"
variable in that it's not bound to any specific instance of A,
but all the instances will use the one and same variable, and in
fact you can access it without an instance of A. The only difference
with a global variable is that 'i' is inside the scope of A and
can only be accessed through A (and it will not cause name collisions
outside of A).
A static variable inside a function is similar: It behaves like it
was a global variable, except that its visibility will be limited to
be inside that function. There's one other major difference with
global variables, though: It will be initialized the first time the
function is called (instead of some time before main() is called).
In practice this means that a static variable inside a function
will preserve its value between function calls.
For example:
void foo()
{
static int i = 0;
++i;
std::cout << i << std::endl;
}
'i' will behave like it was a global variable: The "++i" will
increment its value, and it will preserve that new value even after
the function is exited. The next time the function is called 'i' will
have the value 1 (which is then incremented to 2), and so on.
The initialization to 0 will be performed only once (not each time
the function is called).
The small twist is, as already mentioned, that 'i' will be initialized
the first time foo() is called. That allows doing something like:
void foo(int value)
{
static int i = value;
...
}
The 'i' variable will be initialized to the value of the function
parameter only the first time the function is called. Subsequent calls
will not modify it. (Naturally if you want it to initialize it each
time, obviously don't use 'static'.)
Why are static variables useful? Static member variables of a class
are called "class members". It's sometimes useful to have variables
which are common to all instances of a class, but which scope is
limited to be inside that class (so as to not to garbage the global
scope).
In my experience static variables inside functions are less useful
(mainly it's a historical feature from C) because usually what you
would want to do with a static function variable can be better done
with a class, but I suppose some people have found them useful too.

Hello Juha ,

Thanks for give me answer but my question is Static Object
*object is somewhat a synonym for variable. you might mean a pointer I
guess.*
>
class Foo
{

...

};

int main()
{
static Foo *static_object;
.........
.........
return 0;

}

What's the use to create this type of object?- Hide quoted text -
It does not seem to be wise to put static stuff in main function but a
static pointer might aid some OS resource or some thing that does not
simply belong to the current process or an array of undeterminate size
or a null terminated C string,otherwise I would prefer a normal
variable(object).

Aug 1 '07 #10
"Pramod" writes:
On Jul 31, 3:32 pm, Juha Nieminen <nos...@thanks.invalidwrote:
>Pramod wrote:
int main()
{
static Foo *static_object;

I think I answered that question?

will you explain me again about static object?? please
I think the only part of the answer you got that applies in this example, is
that the system attempts to give Foo an initial value. This is because main
is a "special" function in that the only exit is at the end of the program's
life.

I suggest you post a less stripped down example of something that is claimed
by someone to do something useful and interesting, I would not use the word
"object" in the question. The more specific your question is, the more
useful the answer is likely to be. I skimmed the answer you were given and
it looks like it is probably a comprehensive answer. My guess is that it
was *too* comprehensive for what you really want to know - you don't know
what part of the answer to focus on.
Aug 1 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
6
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.