Solved

Class method or Instance method

Posted on 2002-03-08
11
215 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
  • 5
  • 5
11 Comments
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 9

Expert Comment

by:Venci75
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry for late reply, thanks for your help, I guess its more of a design problem and knowing what i want.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now