Results 1 to 7 of 7
  1. #1
    sat123stud is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    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.

  3. #3
    srrehman is offline Member
    Join Date
    Feb 2011
    Location
    PAKISTAN
    Posts
    4
    Rep Power
    0

    Default

    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;

    }

    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    Quote Originally Posted by srrehman View Post
    Hi!!! Java Static Constructor is just a static block of code which is placed any where in java class .
    No, that's a static initializer.

    There's no such thing as a static constructor in Java.

    db

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by srrehman View Post
    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
    That is not a constructor at all; it is a static initialization block.

    kind regards,

    Jos

    edit: *darn* too slow again and I already had my espresso; I consider this a lost day ;-)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    *kaboom*

    db

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    *kaboom*

    db
    Hey, your scientific progress just went off ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  2. static and a simple constructor
    By bigj in forum New To Java
    Replies: 8
    Last Post: 01-29-2010, 05:00 PM
  3. [SOLVED] static block or constructor?
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 05-27-2008, 02:21 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •