static method

Hi ,

if i have a method static:

public static String authenticate ( String var1, String var2)


1)do i need to instantiate it with "new" command

2) whats the benefit of making the method static

30 what are the disadvantages of making the method static

thanks
RC
royalcyberAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harisha M GEngineerCommented:
Hi royalcyber,
    1) If you want.
    2) You can call the method without creating the object of the class.
    3) If you don't want, skip it, there is no disadvantage.

Bye
---
Harish
Harisha M GEngineerCommented:
royalcyber,
    For example if you have a class Base with a static method Index, then you don't have to create an object of the class Base for calling the function Index

For ex: if not static, Base b = new Base(); b.Index();
If static, Base::Index();
iRadioDevCommented:
doronbCommented:
The concept of static methods goes hand in hand with the Singleton design
pattern.  Singleton classes often create their own instances, and prevent
creation of their instances outsidet the scope of the Singleton class itself by
declaring their only constructor private.

After a Singleton class has created its own instance, or skipped this part, most
often the method of communication with Static classes would be through their
static methods.

A solid example of static method and the singleton pattern is interaction with a
Database through JDBC:

public class DbManager {
  // The JDBC Connection to use..
  private Connection connection;
  // A singleton instance for the DbManager class..
  private final static DbManager self = new DbManager();
  // SQL Prepared Statements to use when processing client requests..
  private PreparedStatement psGetCustomer, psListAllCustomers, psListAllNegative, psAddNewClient, psDeleteClient, psUpdateBalance;

  private DbManager() {
    try {
      // Try to load the JDBC driver class for ODBC support..
      if (Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") != null) {
        // Obtain a connection to the MS-Access Data Source as configured in the ODBC Management Console..
        connection = DriverManager.getConnection("jdbc:odbc:Bank", null, null);
        // Did we obtain a Connection?
        if (connection != null) {
          // Set the connection's auto-commit property to false because we wan't to commit changes ourselves..
          connection.setAutoCommit(false);
          // Prepare the SQL statements for use..
          psListAllCustomers = connection.prepareStatement("SELECT * FROM Accounts");
          psListAllNegative = connection.prepareStatement("SELECT * FROM Accounts WHERE (\"Account Balance\" < 0)");
          psAddNewClient = connection.prepareStatement("INSERT INTO Accounts (\"Customer Number\", \"First Name\", SurName, \"Account Balance\") VALUES (?, ?, ?, 0.0)");
          psDeleteClient = connection.prepareStatement("DELETE FROM Accounts WHERE (\"Customer Number\" = ?)");
          psGetCustomer = connection.prepareStatement("SELECT * FROM Accounts WHERE (\"Customer Number\" = ?)");
          psUpdateBalance = connection.prepareStatement("UPDATE Accounts SET \"Account Balance\" = ? WHERE \"Customer Number\" = ?");
        }
      }
    } catch (SQLException ex) {
      connection = null;
      ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
      ex.printStackTrace();
    }
  }

  // Commit all changes and close the Database connection..
  public static void close() {
    try {
      // Commit all the changes to the database..
      self.connection.commit();
      // Close the database connection..
      self.connection.close();
    } catch (SQLException ex) {
      ex.printStackTrace();
    }
  }

  // Add a new CustomerAccount to the Database..
  public static CustomerAccount addNewCustomer(String firstName, String surName) {}

  // Deposit money into a CustomerAccount..
  public static CustomerAccount deposit(String customerAccount, double amount) {}

  // Withdraw money from a CustomerAccount..
  public static CustomerAccount withdraw(String customerAccount, double amount) {}

  // Remove a CustomerAccount..
  public static int deleteCustomer(String customerAccount) {}

  // Return an array of all CustomerAccounts in the Database..
  public static CustomerAccount[] getAllCustomers() {}

  // Return an array of all CustomerAccounts with negative balance in the Database..
  public static CustomerAccount[] getNegativeCustomers() {}
}

The class DbManager can be used directly by application code now to manage Bank Accounts.
The application can use the DbManager through its static methods, which leaves the application
with one less task to manage, the interaction with the Database is the sole responsability of the
DbManager class.  The Database connection and the prepared SQL statements are all used and
managed by the DbManager class.

There certainly are circumstances where static methods and the Singleton pattern should be used
but not every case requires them.  As for disadvantages, the only one is that you should always
remember that a static method in a class means that the class usually does not maintain a state
that is a part of a process.  This means that if there are any static methods in a class, that class
often will not contain any private data-memebers that are a part of the class's state, or if there
are actual data members, and therefor, a state to manage, then there is only ONE state that is
managed by the class.  A Singleton class -- or class with static methods -- usually manages just
one state per application.  If a class with static methods must manage more than one state for
the tasks it is responsible for, then usually these states (their data-members) would be held in
some sort of cache with a key attached to each state object, usually a Hashtable.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
doronbCommented:
Thanks :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.