Java

 

 

 

 

Java - Class - Basic

 

 

Two Classes in separate files

 

 

 

 

MyPrint.java

PrintUtility.java

public class MyPrint {

 

    public static void main(String[] args) {

 

        PrintUtility prt = new PrintUtility();  

        prt.printString("Hello World");

    }

 

}

public class PrintUtility {

    

    public void printString(String s) {

        System.out.println(s);

    }

    

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Two Classes in single file

 

 

 

 

MyPrint.java

class PrintUtility {

    

    public void printString(String s) {

        System.out.println(s);

    }

    

}

 

public class MyPrint {

 

    public static void main(String[] args) {

 

        PrintUtility prt = new PrintUtility();  

        prt.printString("Hello World");

    }

 

}

 

 

 

 

 

 

 

 

 

 

 

Class Structure

 

 

 

 

 

ClassTest.java

class ClassTemplate {

    

    private int m_Variable = 0;

    

    ClassTemplate()

    {

        System.out.println("ClassTemplate()");

    };

    

    public void setVariable(int v)

    {

        m_Variable = v;

    };

    

    public int getVariable()

    {

        return m_Variable;

    };

    

    public String toString() {

        return "Class : ClassTemplate";

    }

    

 

}

 

public class ClassTest {

 

    public static void main(String[] args) {

 

 

        ClassTemplate ct = new ClassTemplate();

        

        System.out.println(ct.getVariable());

        

        ct.setVariable(10); 

        

        System.out.println(ct.getVariable());

        System.out.println(ct.toString());

    }

 

}

 

 

    C:\temp>java ClassTest

     

    ClassTemplate()

    0

    10

    Class : ClassTemplate