?
Solved

what can be possible reasons for not using static at class level in a project...

Posted on 2005-05-04
10
Medium Priority
?
250 Views
Last Modified: 2010-03-31
what can be possible reasons for not using static at class level in a internet website project...?i am working on one of these project and i am new,my Guide has asked me this question to see what i know about application,and i am new to it...500 points to the exact one..pls be fast i am waiting online
0
Comment
Question by:DilrajSingh123
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 13925055
I have used static in web projects at Servlet level sometimes. I don't see any reasons for completely never using 'static' in a class in a web-project. Is there any specific context to which you are referring?
0
 

Author Comment

by:DilrajSingh123
ID: 13925076
i have created a page that is accessed through a link in other page,my page is kind of template for email,in which customer can set a email template and this template is stored in database.....i have a field ref_no that i made private static but my guide says it has to be only private and static is not needed,i want to know when static is needed and when we should avoid static?
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 13925118

First you have to understand what static means exactly:

If a field or a method is static it means that it belongs to the class, not to any instance of a class.

So you use static only when you dont have an instance of class, that is, when you dont have the actual object, only the class itself.

For example, if you have class A, which has a static field_x:

class A
{
   public static int field_x;
}

And then create two objects:

A a1= new A();
A a2 = new A();

And then change field_x in through one of the objects, then the field_x will actually be changed in all of objects, because it actually belongs to the class, NOT to the object.
Like this:

a1.field_x = 5;
/// a2.field_x is now also 5

And you would get the same effect, if you changed the field_x value through class, like this:

A.field_x = 5;
/// both a1.field_x and a2.field_x are now 5

Static is usually used for methods which dont need any object, like methods in class Math.




















0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DilrajSingh123
ID: 13925131
I UNDERSTOOD STATIC WITH REGARDS TO FUNCTIONS...but where actually static fields are used..can u pls give me one example where we need only class and static fields?
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 13925133

And in your case field ref_no should most probably belong to the object, not to the class, that is why it should not be static.

From the description above you can see, that if you change that value for one object it will actually be changed on the class, therefore on all objects of that class.
0
 
LVL 17

Assisted Solution

by:Dejan Pažin
Dejan Pažin earned 400 total points
ID: 13925150

One example (of many), well known, is Singleton pattern. That  is a class that ensures only one object of a certain class is generated.

It works like this: the class has static method getInstance(), which returns an instnce of this class. In order to have only one instance it needs a private reference called instance, that holds that single object. Now, since the mehtod getInstance() is static (and it MUST be static, cause we dont have an object at start), also the reference MUST be static. That is because from static methods you can naturally only acces static members.

class Singleton
{
  private static Singleton instance;
  private static getInstance()
  {
     if (instance == null) instance = new Singleton;
     return instance;
  }

  private Singleton();
}
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 1400 total points
ID: 13925173
Static variables are dangerous in web applications, because they accessable by ALL users of the web application. They should only be used to hold constant or non-changing data.  The reason is that a web application is a multi-user and multithreaded appliction.  Multiple users are using the application potentially at the same time.  Here is an example of how things can go wrong.

class helloServlet extends HttpServlet
{
   doGet(...)
   {
           NameBean b=new NameBean()
           
           b.setName(request.getParameter("name"));

           response.getOutputStream().write("hello, "+b.getName());
   }
}

class NameBean
{
  private static String name;
  public void setName(String n){name=n;}
  public String getName() {return name;}
}

Two users, Bill and Bob, submit a request to this simple servlet at the same time.  This is the sequence of events:
   1. Bill calls setName("Bill") which sets the static variable to "Bill"
   2. Bob calls setName("Bob") which sets the static variable to "Bob"
   3. Bill writes the response of b.getName() which is "Bob"
   4. Bob writes the response of b.getName() which is "Bob"
   5. Bill stops using the web page because he thinks the programmer is stupid!!!!!

By removing the static, each user will get their own PRIVATE instance of the name variable and the above won't happen.

0
 

Author Comment

by:DilrajSingh123
ID: 13925260
beautiful Mogalmanic u r definitly not maniac...u r to the point in ur answer also dejanpazin gave me a good knowledge,i will divide points between u and dejanpazin
..and ofcourse u will get more points...can anyone of you give me online site of patterns in java?..full length tutorial will be great,if u do this i will create new question and give u 500 points for this..can u?
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 200 total points
ID: 13925309
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 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