473,387 Members | 1,549 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,387 software developers and data experts.

Statics

168 100+
Can anyone help ?

1)where we need to use static methods & static variable in a real time projects?

2)How to Count total numbers of object created by that class in java by using static variable's ?
Jan 31 '08 #1
2 1486
r035198x
13,262 8TB
Can anyone help ?

1)where we need to use static methods & static variable in a real time projects?

2)How to Count total numbers of object created by that class in java by using static variable's ?
Static variables are shared among objects of the same class.
If you have a class called MyClass with a static variable called myVariable, then there's only ever one copy of myVariable no matter how many objects you create of type MyClass. So all MyClass type objects share the same myVariable.
To count how many instances of a class are created, you need to have a static variable in that class and increment its value everytime a new object is created from that class. A possible place to put the code for the incrementing is in an instance initializer block.
Jan 31 '08 #2
BigDaddyLH
1,216 Expert 1GB
Newbies tend to write too many static members. As a rule of thumb I suggest:
  1. Start with only one static method, your main (if your code requires a main). Try to keep your main as short as possible -- a long main encourages you to define other static members.
  2. Start with "constants" as your only static fields:
    Expand|Select|Wrap|Line Numbers
    1. public static final long MY_CONSTANT = 1;
  3. What other fields must be static? It shouldn't be because it's convenient to do so, but because it's logically wrong not to! The example of counting the number of instances generated of a class is one example, but it is a contrived one -- I can't imagine needing to do that in a real application.
  4. What other methods must be static? Simple utility methods are a candidate. See javax.swing.SwingUtilities, for example.
  5. The Singleton Pattern is an example requiring static members, but it's also the most overused/abused design pattern. Again use it because it's logically wrong not to, not because it's easy to do.
Jan 31 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Spacen Jasset | last post by:
Since it says in the standard that the order of initialization of variables is indeterminate, does this also apply to a static within a function? e.g. int GetInt() { static int test = 458;...
4
by: Martin | last post by:
I have some classes with statics public variables.... the problem is that the gcc compiles fine the files but in the linking process gcc says "multiple definitions of MyClass::Variable" or...
5
by: Stuart MacMartin | last post by:
I have a problem with static lifetime (order of destruction of statics within different cpp files). I have a workaround that happens to work in my case. I'd like to know if this is luck or...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
3
by: DR | last post by:
I heard there is some trick to referencing statics in C# CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a little...
3
by: DR | last post by:
I heard there is some trick to referencing statics in VB.NET CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a...
3
by: Peter | last post by:
Hi I can create types from dynamic loaaded assemblies (Assembly.Load) with Assembly.GetTyp( <typename) - OK I need a reference to the assembly (just Type.GetType() do not work ) now my...
1
by: tropos | last post by:
I work on a large Apache plugin for an Internet Bank. Logging shows that many or all static objects in the plugin are being initialised twice at startup time. The platform is C++, AIX Unix,...
8
by: rashmiharitas | last post by:
where are statics variables stored? static variables and static methods can be used without creation of objects so in which part of memory is static stored plz help me
3
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... At least by the group title, this seems like a question for dotnet.framework.aspnet.caching but that group seems pretty slow. I'm trying to sort things out with a co-worker. We've got...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.