Monday, July 30, 2018

Difference between static method and instance method

Instance method are methods which require an object of its class to be created before it can be called. To invoke a instance method, we have to create an Object of the class in within which it defined.
public void geek(String name)
{
 // code to be executed....
}
// Return type can be int, float String or user defined data type.
Memory allocation: These methods themselves are stored in Permanent Generation space of heap but the parameters (arguments passed to them) and their local variables and the value to be returned are allocated in stack. They can be called within the same class in which they reside or from the different classes defined either in the same package or other packages depend on the access typeprovided to the desired instance method.
Important Points:
  • Instance method(s) belong to the Object of the class not to the class i.e. they can be called after creating the Object of the class.
  • Every individual Object created from the class has its own copy of the instance method(s) of that class.
  • They can be overridden since they are resolved using dynamic binding at run time.
class Foo{
     
    String name = "";
     
    // Instance method to be called within the same class or
    // from a another class defined in the same package
    // or in different package.
    public void geek(String name){
         
        this.name = name;
    }
}
 
class GFG {
    public static void main (String[] args) {
     
        // create an instance of the class.
        Foo ob = new Foo();
          
        // calling an instance method in the class 'Foo'.
        ob.geek("GeeksforGeeks");
        System.out.println(ob.name);
    }
}

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
public static void geek(String name)
{
 // code to be executed....
}

// Must have static modifier in their declaration.
// Return type can be int, float, String or user defined data type.

Memory Allocation: They are stored in Permanent Generation space of heap as they are associated to the class in which they reside not to the objects of that class. But their local variables and the passed argument(s) to them are stored in the stack. Since they belong to the class so they can be called to without creating the object of the class.
Important Points:
  • Static method(s) are associated to the class in which they reside i.e. they can be called even without creating an instance of the class i.e ClassName.methodName(args).
  • They are designed with aim to be shared among all Objects created from the same class.
  • Static methods can not be overridden. But can be overloaded since they are resolved using static binding by compiler at compile time.
Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
public static void geek(String name)
{
 // code to be executed....
}

// Must have static modifier in their declaration.
// Return type can be int, float, String or user defined data type.

Memory Allocation: They are stored in Permanent Generation space of heap as they are associated to the class in which they reside not to the objects of that class. But their local variables and the passed argument(s) to them are stored in the stack. Since they belong to the class so they can be called to without creating the object of the class.
Important Points:
  • Static method(s) are associated to the class in which they reside i.e. they can be called even without creating an instance of the class i.e ClassName.methodName(args).
  • They are designed with aim to be shared among all Objects created from the same class.
  • Static methods can not be overridden. But can be overloaded since they are resolved using static binding by compiler at compile time.
class Geek{
     
    public static String geekName = "";
     
    public static void geek(String name){
         
        geekName = name;
    }
}
 
class GFG {
    public static void main (String[] args) {
         
        // Accessing the static method geek() and
        // field by class name itself.
        Geek.geek("vaibhav");
        System.out.println(Geek.geekName);
        
        // Accessing the static method geek() by using Object's reference.
        Geek obj = new Geek();
        obj.geek("mohit");
        System.out.println(obj.geekName);  
         
        
    }
}


When to use static methods ??
  • When you have code that can be shared across all instances of the same class, put that portion of code into static method.
  • They are basically used to access static field(s) of the class.
 Instance method vs Static method
  • Instance method can access the instance methods and instance variables directly.
  • Instance method can access static variables and static methods directly.
  • Static methods can access the static variables and static methods directly.
  • Static methods can’t access instance methods and instance variables directly. They must use reference to object. And static method can’t use this keyword as there is no instance for ‘this’ to refer to.

Difference between index and index hint

Index:
When you use the index keyword in a select statement the kernel will translate this to a order by command and the database optimizer will chose the best index to actually use. 


Example: select * from InventTable index GroupItemIdx will generate the following SQL statement to the database:
SELECT A.ITEMGROUPID, A.ITEMID, A.ITEMNAME,.... FROM INVENTTABLE A ORDER BY A.ITEMGROUPID, A.ITEMID

The Index ItemGroupIdx of the InventTable exactly contains the two fields ItemGroupID and ItemId (in that order). Using "index", you still give the control of which index to use to the database optimizer. So, if the optimizer finds a better index to use, it will use it.


Index hint:
When you chose to use the index hint keyword in your select statement, Ax will force the database to use the chosen index.


Example: select * from InventTable index hint GroupItemIdx will generate the following SQL statement to the database:
SELECT /*+ INDEX(A I_175GROUPITEMIDX) */ A.ITEMGROUPID, A.ITEMID, A.ITEMNAME,.... FROM INVENTTABLE A

Using "index hint", you take away the control of which index to use from the database optimizer. So, if there may be a better index, the database will not use it.



Conclusion:
Adding the "index" statement to an Axapta select, it does NOT mean that this index will be used by the database. What it DOES mean is that Axapta will send an "order by" to the database.
Adding the "index hint" statement to an Axapta select, it DOES mean that this index will be used by the database (and no other one).

How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// ...