Welcome to TTKS/KTQN

CLICK HERE TO OPEN

Monday, August 10, 2015

HOW TO CALL A METHOD OR FUNCTION IN JAVA.

PHƯƠNG PHÁP CALL MỘT METHOD TRONG JAVA PROGRAM
                                     NĂM PHƯƠNG PHÁP CĂN BẢN.
1-   Nêu method là static thì call nó trực tiếp trong main method.
Thí du.
 
class Rextester{
   static void display(){
   System.out.println("Hello Friends ! You call me directly from inside main method\nbecause I am static");
}
    public static void main(String[] args){
                         display();
   }
}
Output.
Compilation time: 0.82 sec, absolute running time: 0.14 sec, cpu time: 0.07 seCompilation time: 0.82 sec, absolute running time: 0.14 sec,
cpu time: 0.07 sec, memory peak: 28 Mb, absolute service time: 0.97 sec
 
Hello Friends ! You call me directly from inside main method
because I am static

2- Nêu method là non-static , muốn call nó phải tạo một object cho class trong main method.

Thí du.
 
class Rextester{
    void display(){
   System.out.println("Hello Friends ! You must create an object for class inside main method to call me\nbecause I am non-static");
}
    public static void main(String[] args){
                        Rextester rx = new Rextester();
                         rx. display();
   }
}
Output.
Compilation time: 0.82 sec, absolute running time: 0.13 sec, 
cpu time: 0.06 sec, memory peak: 27 Mb, absolute service time: 0.95 sec
 
Hello Friends ! You must create an object for class inside main method
To call me because I am non-static

3-   Nếu method là non-static chứa trong Inner class hay còn gọi Member class , muốn call nó phải tạo trong main method một combined object oi của Outer class và Inner class như sau :
                           Outer.Inner  oi = new Outer().new Inner();
Thí du.
 
class Rextester {   
 static{
System.out.println("HOW TO CALL A METHOD WRITTEN INSIDE THE INNER CLASS ? ");
}
class Inner {  
          void display() {  
 System.out.println("I am the method in the Inner class.\n You must create an  object combining the Outer class with the Inner class to call me ");  
 
  public static void main(String[] args){         
Rextester.Inner  oi = new  Rextester().new Inner();
                            oi.display();
}}
Output.
  Compilation time: 1.04 sec, absolute running time: 0.13 sec, 
cpu time: 0.07 sec, memory peak: 28 Mb, absolute service time: 1.18 sec
 
HOW TO CALL A METHOD WRITTEN INSIDE THE INNER CLASS ? 
I am the method in the Inner class.

 You must create an  object combining the Outer class with the Inner class to call me 

4- Trong class Inner viết method void display().Muốn call method nầy thì tạo thêm một method nữa thí dụ void run().Trong void run() tạo một object cho class Inner , rồi call method display() trong void run().
 
class Rextester {   
 static{
System.out.println("HOW TO CALL A METHOD WRITTEN INSIDE THE INNER CLASS ? ");
}
class Inner {  
          void display() {  
 System.out.println("I am the method in the Inner class.\nCreate an object for Inner class inside the method run to call me. ");  }  
 
  public static void main(String[] args){         
Rextester  rx = new  Rextester();
                            rx.run();}
 
           void run(){
             Inner in = new Inner();
                    in.display();}
}
Output.
 Compilation time: 0.83 sec, absolute running time: 0.13 sec, cpu time: 0.1 sec, memory peak: 27 Mb, absolute service time: 0.96 sec
 
HOW TO CALL A METHOD WRITTEN INSIDE THE INNER CLASS ? 
I am the method in the Inner class.
Create an object for Inner class inside the method run to call me.
5-Nếu method display() viết trong static class Nested.Tạo object cho class Nested trong main method rồi call display() .
Thí dụ
   class Rextester{
   static class Nested{  
   void display(){    // Method having no return 
System.out.println(" Function display() is inside class Nested");}
}
    public static void main(String[] args){
           Nested ns= new Nested(); 
                        ns.display(); }
}
output.
Compilation time: 0.83 sec, absolute running time: 0.14 sec, 
cpu time: 0.09 sec, memory peak: 21 Mb, absolute service time: 0.99 sec
 
Function display() is inside class Nested
 
GHI CHÚ.
Java  programming chấp nhận trong mỗi class, chúng ta có thể viết thêm một hay nhiều classes khác có một tên chung là nested class.
Có 2 loại nested class :  
* static nested class và non- static nested class.
* non-static nested class còn được gọi là INNER CLASS hoặc MEMBER CLASS.
* class ngoài cùng gọi là OUTER CLASS.
* INNER CLASS viết trong OUTER CLASS.
* Trong bài nầy,người viết chọn OUTER CLASS là class Rextester để có thể xử dụng compiler của Rextester.com   
*Taị sao chúng ta cần dùng Inner class hay Nested class? Vì muốn program được đơn giản và có tổ chức rõ ràng dễ kiểm soát(Simple and Concise),mỗi class làm một phần việc riêng.

Saturday, July 18, 2015

JAVA PROGRAM CÓ 2 INNER CLASSES

JAVA PROGRAM HAVING 2 INNER CLASSES.
   class Inner1 { … } and  class Inner2 { … }
 
1-  Compiler của Ideone.com phải dùng outer class là class Main.
 
public class Main {  
        String outerStr = " String in the class outer : THÂN CHÀO QUÝ BAN !\n"  ;
class Inner1 {
         String inner1Str = " String in the class Inner1 : \n" ;     
        String display1(String parameter) { 
        return   outerStr+ inner1Str + parameter ;  }       
        }   
    public static void main(String[] args) {
        Main outer = new Main();  
                        outer.run() ;
        Main outer1 = new Main();  
                         outer1.go() ;  }   
       void run() {  
        Inner1 local = new Inner1();
        System.out.println(local.display1(" Hola Amigaso ! COMMO ESTA ?"));
        }
       void go() {  
        Inner2 in2 = new Inner2();
        System.out.println(in2.display2("Hello Friends ! HOW ARE YOU ?"));
        }
 
class Inner2 { 
        String inner2Str =  "  String in the class Inner2 :\n" ;     
        String display2(String parameter) { 
        return inner2Str +  parameter ;   }        
 
     }
}
Output.
SUCCESS
String in the class outer : THÂN CHÀO QUÝ BAN !
 String in the class Inner1 : 
 Hola Amigaso ! COMMO ESTA ?
  String in the class Inner2 :
Hello Friends ! HOW ARE YOU ?
                   
2- Compiler của Rextester.com
 
class Rextester {  
        String outerStr = " String in the class outer : THÂN CH̀ÀO QUÝ BAN ! \n"  ;
class Inner1 {
         String inner1Str = " String in the class Inner1 :\n" ;     
        String display1(String parameter) { 
        return  outerStr + inner1Str + parameter }       
        }   
    public static void main(String[] args) {
        Rextester outer = new Rextester();  
                        outer.run() ;
        Rextester outer1 = new Rextester();  
                         outer1.go() ;  }   
       void run() 
        Inner1 local = new Inner1();
        System.out.println(local.display1(" Hola Amigaso ! COMMO ESTA ?"));
             }
       void go() 
        Inner2 in2 = new Inner2();
        System.out.println(in2.display2("Hello Friends ! HOW ARE YOU ?"));
        }
class Inner2 { 
        String inner2Str =  "  String in the class Inner2 :\n" ;     
        String display2(String parameter) { 
        return inner2Str +  parameter  }              
     }
}
output.
Compilation time: 0.82 sec, absolute running time: 0.14 sec,
 cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.97 sec
String in the class outer : THÂN CHÀO QUÝ BAN !
 String in the class Inner1 : 
 Hola Amigaso ! COMMO ESTA ?
  String in the class Inner2 :
Hello Friends ! HOW ARE YOU ?

Saturday, July 4, 2015

CALLING A JAVA METHOD FROM ANOTHER METHOD

HOW TO CALL A METHOD FROM ANOTHER METHOD WITHIN THE SAME MEMBER CLASS OR INNER CLASS.
Ghi chú hướng dẫn.
Trong main method, chúng ta có thể call những methods ở ngoài với điều kiện những methods đó phải static.
Nếu những methods ở ngoài không static thì phải taọ object cho class rồi mới call được .
Thí dụ: 1- non –static void run() ở trong class Rextester.
                    Rextester outer = new Rextester();  
                                    outer.run();
            2- non-static String display() ở trong class Inner. 
                    Inner  In = new Inner();
                                In.display();                    
                             -----------------
 1-Tạo method mới rồi dùng nó để call method khác .
class Rextester {  
        String outerStr = " This is the String of Outer class :"  ;
 class Inner {
        String innerStr =  "This is the String of Inner class :" ;     
 
/* This method display() will be called by method run() below */       
        String display(String parameter) {        
               return  innerStr +  outerStr + parameter ;}       
    }
public static void main(String[] args) {
 /*  create an object inside main method and call 
          method run() from main method*/
        Rextester outer = new Rextester();  
                        outer.run();
    }   
       void run() { 
             /* create an object outside main method and call 
                  method display()written inside class Inner*/
                       Inner  In = new Inner();
        System.out.println(In.display(" Hola Amigasos ! Chào Quý Ban "));
    }
 }
OUTPUT.
 
Compilation time: 0.72 sec, absolute running time: 0.13 sec,
 cpu time: 0.07 sec, memory peak: 23 Mb, absolute service time: 0.86 sec
 
This is the String of Inner class : This is the String of Outer class : Hola Amigasos ! Chào Quý Ban. 
 
 
 
2- Không tạo method mới.Call trực tiếp từ main method.
class Rextester {   
 int x = 2015;  
 private String str1 = "NHƯNG MUÀ HÈ QUÁ KHỨ,HUÊ";  
 public String str2 = "DO YOU STILL REMEMBER ?";  
 protected String str3 = "\n CHIA TAY TỪ THUỎ RA TRƯÒNG.\n CUỐI ĐƠI NỐI LAI BỐN PHƯONG MỘT NHÀ.";  
class Inner {  
          void display() {  
 System.out.println( x + "\n " + str1 + "\n " + str2+ "\n " + str3);  
 
  public static void main(String[] args){         
Rextester.Inner  oi = new  Rextester().new Inner();
oi.display();
 System.out.println(" KÍNH CHÚC QUÝ BAN KHOE MANH LUÔN. ");
}}
OUTPUT.
2015
 NHƯNG MUÀ HÈ QUÁ KHỨ,HUÊ
 DO YOU STILL REMEMBER ?
 Ơ CUÔI CÙNG ĐÃ́T. 
 ĐOÀN TRAI NON HỚN HỞ RỦ NHAU VỀ.
 CHÍN MƯOI NGÀY NHẢY NHÓT Ở MIỀN QUÊ,….>
 CHIA TAY TỪ THUỎ RA TRƯÒNG.
 CUỐI ĐƠI NỐI LAI BỐN PHƯONG MỘT NHÀ.
 KÍNH CHÚC QUÝ BAN KHOE MANH LUÔN.