Saturday, 7 July 2018

learn how to use variable value in main class from other class


learn how to use variable value in main class from other class
if you make a java project need more variable values use in
main program here we give a simple program that show how
to call variable value in main runnable class from other class
firstly, you need a main program, you create main program
then create new object with other class name and call variable
value.
main class  java code
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package addtwoclass;

/**
 *
 * @author rajesh kumar shukla
 */
public class Addtwoclass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        nextclass nn=new nextclass();
        int sum=nn.total;
        System.out.println("sums of a and b is"+sum);
    }
    
}
other class java code
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package addtwoclass;

/**
 *
 * @author rajesh kumar shukla
 */
public class nextclass {
    int a=5;
    int b=6;
    int total=a+b;
    
}

0 comments:

Post a Comment