Solved

Class method or Instance method

Posted on 2002-03-08
11
227 Views
Last Modified: 2013-11-23
I am deciding between
1) creating an object with its own instance variables and methods to do some processing
2) develope a class with only static methods and do the some processing.

e.g

public class Circle
{
    private int pi = 3.142;
    private int radius;
   
    public Circle(int r)
    {
        radius = r;
    }
   
    public int area()
    {
        return pi*r*r;
    }
}

OR

public class Circle
{
    private static pi = 3.142;

    public int area(int r)
    {
        return pi*r*r;
    }
}


Is it good OO design to have class that only have static methods? When would one normally use such class instead of creating object?

Is using such a class with only static methods much cheaper than creating objects?If so why do we not just use static method class to save memory usage?
0
Comment
Question by:jerntat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 6849896
The static variables are global variables (constants) for your program. In the clase of pi - it is better to use static variable, because you will have only one global instane of this variable in you program. Otherwise - every single object of class Circle will have its own pi variable, which principally is not correct. I even suggest using:
private static final pi = 3.142;
this makes your pi a constant.

The radius variable is not static, because otherwize - all instances of your Circle class will have the same radious, which is not good.
0
 

Author Comment

by:jerntat
ID: 6849946
I agree with setting pi as static for the first case.

However, in the second case there wont be any instances of Circle, but just a Circle class that is able to calculate the area of a circle given the radius.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6849958
Sorry - I didn't get it :)
yes - in my oppinion in the second case you should have:
public class Circle {
   private static pi = 3.142;

   public static int area(int r)
   {
       return pi*r*r;
   }
}

this way you will allow the usage of the Circle class methods without having an instance of this class.
In the 'area' method you are using only static variables and parameters to calculate the value, so it is better to declare it as static. But this won't save memory, because the methods are loaded to the memory only once and after that - used for all instances of your class. You can combine the the both approaches:
public class Circle {
   private static pi = 3.142;
   private int radius;
   public Circle (int r) {radius = r;}

   public static int area()
   {
       return area(radius);
   }
   public static int area(int r)
   {
       return pi*r*r;
   }
}

but this is only for you - to make your class much convenient.
0
Industry Leaders: 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!

 
LVL 92

Expert Comment

by:objects
ID: 6851658
Depends if you want to create instances of Circle's or not, or just have a class for calculating the area or circles. If you want to create instances of Circle's then use approach 1 (but make pi static as mentioned above), but if you only want to be able to calculate the are of circles then use approach 2 (but make the area method static as mentioned above).
0
 

Author Comment

by:jerntat
ID: 6875486
Sorry for late reply. Been trying to figure out what i want. I am actually trying to develope a servlet and had develope a class to handle each request as it is recieved (i'll call this class requestHandler class).The servlet will create a thread to handler each request.

The requestHandler class process the request in a number of steps so I have divided the requestHandler class into a number of methods and a couple other helper class to assist in processing the request.

If I develope the requestHandler class as static. Then there will be alot of parameter passing between private methods inside the helper class going through a number of steps to process the request.

for example:

public class requestHandler
{
   process(par1, par2 par3)
   {
      result1 = processStepOne(par1,par2,par3);
      result2 = processStepTwo(result1,par1,par2);
      result3 = processStepThree(result2,par1,par2);

      return result3;
   }
}

However If I develope the requestHandler class as non-static then i can keep the results as a field member and
initialise the input parameter in a constructor method.

for example:

public class requestHandler
{
   par1;
   par2;
   par3;
   result1;
   result2;
   result3;

   public requestHandler(p1, p2, p3)
   {
      par1 = p1;
      par2 = p2;
      par3 = p3;
   }  

   public process()
   {
       processStepOne();
       processStepTwo();
       result3 = processStepThree();

       return result3;
       
   }
}

In the static method, each thread request will share the single requestHandler that is being created once when the servlet initialise.

This would probably be able to improve performance and memory usage by reducing the amount of object creation.

In the non-static method, I would create a requestHandler object for each thread request.

I feel that this method would make the whole design cleaner and easy to understand and maintain but there would be more object creation.

Some my concerns are:
*Will there be a bottleneck if all the thread request share a single requstHandler object.How are method code
share among instances of an object in JVM?
*Why object creation is much more expensive since all instances of a class share a single Method area storing method codes, the only extra memory each object would use is to store the field members.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6876430
if par1, par2 and par3 are constants - have constant values for all calls of the process() methos - you can declare them as static. In all other cases - you shouldn't do that.
The problem with the creation of new objects is mainly in the time, needed for the creation - not in the used memory.
What you can do is reusing the RequestHandler object created on servlet initialization. This is not a bottleneck, but you must make sure that you RequestHandler class is thread safe.
So - in my oppinion the best choice is:
public class requestHandler
{
  par1;
  par2;
  par3;

  public requestHandler(p1, p2, p3)
  {
     par1 = p1;
     par2 = p2;
     par3 = p3;
  }  

  public process()
  {
      // use method variables for the results
      // of the steps
      result1 = processStepOne();
      result2 = processStepTwo();
      result3 = processStepThree();

      return result3;
     
  }
0
 

Author Comment

by:jerntat
ID: 6878515
Since all the threads are trying to access requestHandler class and requestHandler class only have one copy of par1,par2 and par3 and to make it thread safe, wouldnt there be some sort of bottleneck when one thread have to wait for the previous thread to finish processing with requestHandler?
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6879181
The threads don't need to wait each other, because you don't have class members which could be modified concurenly. Only the class members are critical when two (or more) threads call the process() method in parallel. There is no problem when the threads share (the process() or any other) method code.
0
 

Author Comment

by:jerntat
ID: 6881556
Ok i get it...but i think there is some misunderstanding. Its my mistake in my code:

public class requestHandler
{
  par1;
  par2;
  par3;
  result1;
  result2;
  result3;

  public requestHandler(p1, p2, p3)
  {
     par1 = p1;
     par2 = p2;
     par3 = p3;
  }  

  public process()
  {
      processStepOne();
      processStepTwo();
      result3 = processStepThree();

      return result3;
     
  }
}


for processStepOne(), processStepTwo and processStepThree()
actually uses result from the previous process, that is to say:

private void processStepOne()
{
   //code that uses par1, par2 and par3
   //do some calculation and store the result
   //of calculation as result3

   result1 = somecalculation(par1,par2,par3);
}

private void processStepTwo()
{
  //code that uses result1, par1 and par2
  //do some calculation and store as result2
 
  result2 = somecalculation(result1,par1,par2);
}

and so on.

however since result1 and result2, par1, par2 and par3 are declared as instance variable they are visible to all the methods.Or is this a bad design?
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 100 total points
ID: 6881995
What you must do is to declare
result1, result2, and result3 as a method variables (not class variables):
public process() {
  result1 = processStepOne();
  result2 = processStepTwo(result1);
  result3 = processStepThree(result2);
  return result3;
}
because if you have two threads calling the process method at the same time - the values of these variables will be changed cocurently by the both threads.

Also - from the design point of view - I don't think it is a good idea to have a class level variables to keep an intermediate results of a method.
0
 

Author Comment

by:jerntat
ID: 6924847
Sorry for late reply, thanks for your help, I guess its more of a design problem and knowing what i want.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

756 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