简体   繁体   中英

how to call a method of main class from main class with another class object in JAVA

how to call a method of main class from main class with another class object. Please help me

StudentRecord.java

package com.test;
public class  StudentRecord {
    public String name;  
    public int rollNumber;  
    public String departement; 
    public float    totalMark;
    boolean hasReservation;

    public  StudentRecord(){
        name           = new String("");
        rollNumber     = 0;
        departement    = new String("");
        totalMark      = 0;
        hasReservation = false;
    }
    public String toString() {   
        return "[" + departement+ ","+ name + "," + rollNumber +"]";   
    }
}

Test.java

package com.test;
public class Test { 
    public native static StudentRecord[] getStudentDetails();   
    public static void main(String[] args) {
        System.loadLibrary("Sample");
        int a= 10;
        StudentRecord[] records = getStudentDetails();      
        for(StudentRecord record:records){
            System.out.println("Name:"+record.name);
            System.out.println("Roll Number:"+record.rollNumber);
            System.out.println("Departement:"+record.departement);
            System.out.println("Total Marks:"+record.totalMark);
            System.out.println("Has Reservation:"+record.hasReservation);
        }

    }   
}
 error: cannot find symbol
         StudentRecord[] records = getStudentDetails();  
                     ^
  symbol:   class getStudentDetails
  location: class eventJava
eventJava.java:22: error: cannot find symbol
       for(Record record:records){
                         ^
  symbol:   variable records
  location: class test
2 errors

你可以通过 new Test().getStudentDetails() 而不是 getStudentDetails() 来试试这个,猜这对你有用

StudentRecord.java

package com.test;
public class  StudentRecord {
    public String name;  
    public int rollNumber;  
    public String departement; 
    public float    totalMark;
    boolean hasReservation;

    public  StudentRecord(){
        name           = new String("");
        rollNumber     = 0;
        departement    = new String("");
        totalMark      = 0;
        hasReservation = false;
    }
    public String toString() {   
        return "[" + departement+ ","+ name + "," + rollNumber +"]";   
    }
}

Test.java

package com.test;
public class Test { 
    public native static StudentRecord[] getStudentDetails();   
    public static void main(String[] args) {
        System.loadLibrary("Sample");
        int a= 10;
        StudentRecord[] records = getStudentDetails();      
        for(StudentRecord record:records){
            System.out.println("Name:"+record.name);
            System.out.println("Roll Number:"+record.rollNumber);
            System.out.println("Departement:"+record.departement);
            System.out.println("Total Marks:"+record.totalMark);
            System.out.println("Has Reservation:"+record.hasReservation);
        }

    }   
}

Compulsary two java code in same package

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM