Solved

static objects

Posted on 2003-11-24
5
282 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Saving Tweets with emojis using Twitter4J in a Mysql table 2 73
base64 decode encode 12 119
recursion example 16 111
rhino JavaScript import, load 25 66
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

911 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

19 Experts available now in Live!

Get 1:1 Help Now