Home TechnologyProgrammingJava Subscribe to RSS

Give Me A Instance Example In Java Program. Can You Help?

Answer Question

1 Answer - Sort by: Date | Rating

    Well I am not clear with your Question

    I suppose you want to know the instance of a class in java. I am guessing so...


    public class InstanceDemo {

        int no1;    //class data member of type int
        int no2;    //also called as 'field' of type int in java
        
        String tmp; //class data memberof type String

        //Constructor
        public InstanceDemo(int no1, int no2, String tmp) {
            this.no1 = no1;
            this.no2 = no2;
            this.tmp = tmp;
        }
        
        public void display() {
            System.out.printl name : " + this);
            System.out.printl name : " + this.getClass());
            
            System.out.printl />        
            System.out.printl : " + no1 + " *** and *** " + "no2 : "+ no2);
                    
            // this reference can also be used for a clear idea
            System.out.printl : " + this.tmp);
            System.out.printl />    }
        
        public static void main(String[] args) {
            //creating object
            //here instanceDemo is an Instance of class InstanceDemo
            //instance simply means Object of a class
                   
            InstanceDemo instanceDemo1 = new InstanceDemo(10, 20, "Hello");
            InstanceDemo instanceDemo2 = new InstanceDemo(20, 30, "World!");
            
            //we can create as many instance's as we wish
            //but always remember each instance will have it's own copy of fields
            instanceDemo1.display();
            instanceDemo2.display();
        }   
    }


    1 0

    Rinav  

    answered 1 year ago

    Where did my java code vanish???

    Correct statements :
    Public void display() {
    System.out.println("Instance name : " + this);
    System.out.println("class name : " + this.getClass());

    System.out.println();

    System.out.println("no1 : " + no1 + " *** and *** " + "no2 : "+ no2);

    // this reference can also be used for a clear idea
    System.out.println("tmp : " + this.tmp);
    System.out.println();
    }
    Report
    Rinav

    Rinav

    commented 1 year ago

      More

         
         

        Ask a Question via Twitter

        Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

        Blurtit Store

        Get T-shirts, hoodies, caps and more at the Blurtit store

        Blurtit International