0

I'm new in java an having some trouble in calling of another a function from a class login which records a session user getting the date and time of login.. I would wish to call this function in a sales class where the users name will be retrieved from database table session

      public void Sessiontracker(){
      int day, month, year;
      int second, minute, hour;
      GregorianCalendar date = new GregorianCalendar();

      day = date.get(Calendar.DAY_OF_MONTH);
      month = date.get(Calendar.MONTH);
      year = date.get(Calendar.YEAR); 

      second = date.get(Calendar.SECOND);
      minute = date.get(Calendar.MINUTE);
      hour = date.get(Calendar.HOUR);

      String TheDate = +day+"/"+(month+1)+"/"+year;
      String TheTime =+hour+" : "+minute+" : "+second;
      try{
         String sql2= "insert into     session(Date,Time,Username)values('"+TheDate+"','"+TheTime+"','"+jTextField1.getText()+"' ) ";
            pst=con.prepareStatement(sql2);
            pst.execute();    
      }catch(SQLException | HeadlessException e){
         JOptionPane.showMessageDialog(null, e); 

    }                         
 }

that is the login class extract

  private void Sessionuser (){



    }

I would wish to call the function here

Razib
  • 10,965
  • 11
  • 53
  • 80

2 Answers2

3

I'm not going to tell how you can call your function. But in general you can call other class' public method by association.

Suppose you have the following 2 classes:
class A -

public class A{

   // some private property

   public void methodA(){}
   public static methodStaticA(){} //note methodStaticA() is a static method
}

and another class B -

public class B{

       // some private property

       public void methodB(){}
 }  

Calling Instance Method (non static method):
Now from the third class C you can call methodA() of class A and methodB() of class B (which are know as instance methods) by using association -

public class C{

       // some private property
      private A a;
      private B b;

      public void methodC(){
        a.methodA(); //calling methodA() of class A
        b.methodB(); //calling methodB() of class B
      }
  }

Calling static Method:
Class A have a static method methodStaticA(). In java we don't have to create instance of A to call that static method. From class C we can just call like this -

public class C{

      public void anotherMethodC(){

         A.methodStaticA(); 

      }
  }

Hope it will help.
Thanks.

Razib
  • 10,965
  • 11
  • 53
  • 80
1

A method is either declared as static or not. A static method is invoked on the Class. A normal method is invoked on the instance of a class, also called an object.

So, either you instantiate an instance of your first class with the new operator and invoke the method on that instance:

MyClass instance = new MyClass();
instance.SessionTracker();

Or you declare your method as static by adding the static keyword before the return type void and invoke the method on the class, but then you cannot use non-static fields in your method:

MyClass.SessionTracker();

PS:

a Java developer is talking about methods if the execution is changing the state of objects; and functions if the execution is not changing state of objects

a method's name should start with a lower case

Stefaan Neyts
  • 2,054
  • 1
  • 16
  • 25