473,386 Members | 2,078 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,386 software developers and data experts.

Instance Variable in Static Method

Hi All,
In a Singleton pattern , if we create an instance
variable, and return it, whether this one also be a static variable ,
Can anyone give me insights on the Memory allocation

Thanks in Advance

thomson

May 10 '06 #1
6 3304
I am having a bit of trouble understanding the question but ..

are you asking the memory differences between

public class Singleton {
private something Foo;
public something GetFoo() {
return Foo;
}
//singleton stuff
}

and

public class SomeStaticClass {
public static something Foo;
}

they are pretty much identical performance wise if you already have a
reference to the singleton .. the access to the data internally is slightly
different but not enough that you should be worried about it.

Cheers,

Greg Young
MVP - C#
"thomson" <sa**********@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Hi All,
In a Singleton pattern , if we create an instance
variable, and return it, whether this one also be a static variable ,
Can anyone give me insights on the Memory allocation

Thanks in Advance

thomson

May 10 '06 #2
Hit send too quickly ..

This is a great case of using what makes sense with the design .. there is
very little performance to be gained/lost here.

Cheers,

Greg Young
MVP - C#
"thomson" <sa**********@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Hi All,
In a Singleton pattern , if we create an instance
variable, and return it, whether this one also be a static variable ,
Can anyone give me insights on the Memory allocation

Thanks in Advance

thomson

May 10 '06 #3
Hi Greg,
if i have an instance variable inside the static
method, will it be a static variable?

May 10 '06 #4
With a static method, there is no instance(s) unless you pass one to the
method. There is only the static type and its static members. You can
define a local var in a static method, but that is a local variable with
local scope; not a static or instance field.

--
William Stacey [MVP]

"thomson" <sa**********@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
| Hi Greg,
| if i have an instance variable inside the static
| method, will it be a static variable?
|
May 10 '06 #5
I think Inside Method .. whether its static or not all variables are local
variables and treated as Instance Varible .. oh yes scenerio can become
interesting if you ask that u r passing some variable to static method ..
what will be the effect if it is passed by value and passed by reference.
Cheers

"thomson" wrote:
Hi Greg,
if i have an instance variable inside the static
method, will it be a static variable?

May 10 '06 #6
Hi,

"thomson" <sa**********@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Hi Greg,
if i have an instance variable inside the static
method, will it be a static variable?


I think you are confusing the terms "instance variable" and "local
variable".

Instance variable has an instance scope, all the methods of the instance
can access it, it's created once when the instance is created and is
destroyed when the instance is GC-ed. It's stored in the same memory than
the instance itself.

A local variable is declared inside a method, it's ALWAYS created in the
stack and is create EACH TIME the method is executed. it's destroyed when
the method finish. It's not accesible from another method and do not persist
between method invocations.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
May 10 '06 #7

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

Similar topics

18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
22
by: WXS | last post by:
Sometimes a method in a class requires the use of class instance variables/fields that will not be used outside of the method itself. Currently this means you must create a instance field in the...
11
by: syssyx | last post by:
I have a "CurrentUser" object in session that I want to access from each page of a vb.net website. I can successfully access everything if I include the following at the start of each pageload: ...
5
by: Dinsdale | last post by:
I was discussing class architecture with one of the senior developers at my new job and him and I have a similar idea on how to work with data access and class libraries. That said, our...
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...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
14
by: Jordan Marr | last post by:
I have the following class: class ProvisionCollection { ... private int m_VarianceCount; public int VarianceCount { get { return m_VarianceCount; }
4
by: Jon Skeet [C# MVP] | last post by:
On Aug 11, 5:11 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote: Exactly. My favourite example is Thread.Sleep. Suppose this code were valid (its equivalent in Java is, for example): ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.