Solved

static objects

Posted on 2003-11-24
5
277 Views
Last Modified: 2010-04-16
What are the advantages/disadvantages of making class objects as static ? Give examples in case of either case.
0
Comment
Question by:NeerajVij
5 Comments
 
LVL 2

Accepted Solution

by:
DidierD earned 34 total points
ID: 9816169
0
 
LVL 3

Assisted Solution

by:monkesdb
monkesdb earned 33 total points
ID: 9816228
when you declare an object as static it is the same / shared through all instances of that class.

public class A
{
    public int val;
}

public class B
{
    public static int val = 0; // i think you need to initialise it here, i might be wrong, you could leave this out.
}

public class Main
{
    public static int main(String args[])
    {
        A a1 = new A();
        A a2 = new A();
        a1.val = 1;
        a2.val = 5;
        System.out.println( a1.val + ", " + a2.val );

        B b1 = new B();
        B b2 = new B();
        b1.val = 1;
        b2.val = 5;
        System.out.println( b1.val + ", " + b2.val );

    }
}

when you run this code you will see that in B the variable val is the same in both instances now this is useful and in some cases essential. that, however, is something you would have to determine per problem.
0
 

Assisted Solution

by:CapitalJ
CapitalJ earned 33 total points
ID: 9816394
static means that it is a CLASS variable, instead of an INSTANCE vairable.

public class Test1 {

  public static String str1 = "This is a STATIC variable";
  public String str2 = "This is a non-static variable";

}

public class Test2 {

  public static void main(String[] args) {

    System.out.println(Test1.str1);
    //I'm NOT instantiating an Object of type Test1,
    //I reference the CLASS

    //I can NOT do the following, it won't compile:
    System.out.println(Test1.str2); // WRONG!!!

    //I have to do this:
    Test1 t1 = new Test1();
    System.out.println(t1.str2);

    //I CAN also reference static variables from the OBJECT like:
    System.out.println(t1.str1);
   
  }

}

static variables is used when you want to have acces to them via the CLASS, NOT the OBJECT.

You can reference static variables via the CLASS OR the INSTANCE of that class.
You can ONLY reference non-static variables from an INSTANCE of that class.

Hope this helps
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now