Results 1 to 7 of 7
- 08-29-2011, 09:00 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
what is static constructor in java?
I am having a static variable in a class and assigned it to a value say 10. If i want that when the object of the class is created the value of the static variable is changed to another value.
Is it Possible ? if yes then how
can it be done through static constructor.
- 08-29-2011, 09:35 AM #2
yes, as long your static variable is not final. you can change your variable as long the access modifiers of the variable allow this. but watch out: static variables in a class are called class variables, because these variables are shared among all objects. so if you change the values of a static variable you will change the value of all instances derived from this class.
Last edited by j2me64; 08-29-2011 at 09:44 AM.
- 08-29-2011, 10:21 AM #3
Member
- Join Date
- Feb 2011
- Location
- PAKISTAN
- Posts
- 4
- Rep Power
- 0
Hi!!! Java Static Constructor is just a static block of code which is placed any where in java class .This code includes any thing and code must be enclosed in braces{} with a title of static.i.e
public class MyClass{
..................
static{
int a;
switch(a) {
case 7:
extra = 100;
break;
case 8:
extra = 1000;
break;
case 9:
extra = 10000;
break;
default:
extra = 0;
}
}
- 08-29-2011, 10:28 AM #4
- 08-29-2011, 10:28 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
- 08-29-2011, 12:40 PM #6
*kaboom*
db
- 08-29-2011, 01:08 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,394
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM -
static and a simple constructor
By bigj in forum New To JavaReplies: 8Last Post: 01-29-2010, 05:00 PM -
[SOLVED] static block or constructor?
By JT4NK3D in forum New To JavaReplies: 3Last Post: 05-27-2008, 02:21 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks