SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    calling a class in java

    Hi, anyone know how to call another class in java pls? many thanks in advance


    e.g

    pseudo code:
    class1 {

    //statements

    class2 //call to calss 2



    main()
    {


    }



    }

    class2
    {
    //statements..

    }

    Q: How can I call class2 within class1 pls help!

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Java is object orientated, therefore, you'll need to instantiate (create) a Class2 object in order to use it, as below:
    Code:
    //filename : Class1.java
    public class Class1 {
      public static void main( String[] args ) {
        Class2 newlyCreatedClass2Object = new Class2();
        newlyCreatedClass2Object.sayHello();
      }
    }
    
    class Class2 {
      public void sayHello() {
        System.out.println( "Hello" );
      }
    }

  3. #3
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also don't forget you may access an object methods and properties by extending that class in your class. In other words:

    class FirstClass extends SecondClass {

    out.print(getResult());


    }

    class SecondClass {

    String getResult() {

    return "some string";
    }
    }

    The methods in the extended class are automatically available to the class that extends it (unless it is private)

    May seem a bit much but that's how Java works.

  4. #4
    SitePoint Evangelist borna's Avatar
    Join Date
    Jun 2006
    Location
    Iran, Tehran
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am terribly sorry, what kind of change here is required in order to get this simple program running:
    Code:
    class FirstClass extends SecondClass {
         
        public static void main(String args[]){
            firstClass FirstClass=new FirstClass();        
        }
        
        System.out.println(getResult);
        
    }
    
    class SecondClass {
    
    String getResult() {
    
    return "some string";
    }
    }
    -1/2=999?

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Logan, Utah
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by borna View Post
    I am terribly sorry, what kind of change here is required in order to get this simple program running:
    Code:
    class FirstClass extends SecondClass {
         
        public static void main(String args[]){
            firstClass FirstClass=new FirstClass();        
        }
        
        System.out.println(getResult);
        
    }
    
    class SecondClass {
    
    String getResult() {
    
    return "some string";
    }
    }
    Code:
    class FirstClass extends SecondClass {
         
        public static void main(String args[]){
            FirstClass firstClass=new FirstClass();        
            System.out.println(firstClass.getResult());
        }    
    }
    
    class SecondClass {
    
    String getResult() {
    
    return "some string";
    }
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •