Results 1 to 3 of 3
  1. #1
    dhirajb1989 is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default Output for the following Java Program

    The Question is "Why is the output 00" for the following code snippet even though 10 and 2 have been sent as arguments?

    Java Code:
    class Test{
    
    	int i,j;
    	
    	Test(int i,int j){
    		i=i;
    		j=j;
    	}
    	
    	Test(){
    	};
    	
    	void display(){
    		System.out.println(++i +"Hello"+ ++j);
    	}
    };
    
    class TestE extends Test{
    	int i,j;
    	
    	TestE(int i,int j){
    		super(i,j);
    		i=i;
    		j=j;
    	};
    	
    	void display(){
    		System.out.println(i+""+j);
    	};
    };
    
    class Main{
    
    	public static void main(String args[]){
    		Test T=new TestE(10,2);
    		T.display();
    	}
    };
    Last edited by dhirajb1989; 09-08-2013 at 07:54 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,505
    Rep Power
    5

    Default Re: Output for the following Java Program

    Do you have a question? And please place your code between code tags [code][/code].

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Output for the following Java Program

    Java Code:
    class TestE extends Test{
        int i,j;
         
        TestE(int i,int j){
            super(i,j);
            i=i;
            j=j;
    Your problem is that you have given the instance variables and the parameters the same name. Where you have i=i; that uses the parameter and not the instance variable. Effectively that line of code does nothing as you simply reassign the same value back to the parameter.

Similar Threads

  1. Output of a basic Java program
    By aussieguy0101 in forum New To Java
    Replies: 5
    Last Post: 02-17-2013, 07:52 AM
  2. Wrong Output (Java Program)
    By poupas in forum New To Java
    Replies: 12
    Last Post: 11-28-2010, 04:28 PM
  3. Redirecting output of java program to file
    By dewitrydan in forum New To Java
    Replies: 4
    Last Post: 11-17-2010, 06:58 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  5. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 05:06 PM

Posting Permissions

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