Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Please help to understand whats the wrong with my guess. - Simple program

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Please help to understand whats the wrong with my guess. - Simple program

    Hi
    Please try to understand why this program produce 100 200 as output. I was expecting out put as 100 200 30 40

     
    class MyClass{
     
    int x=100;
    int y=200;
     
    void MyClass(){
    System.out.print(x+" ");
    System.out.print(y+" ");
    x=30;
    y=40;
    }
    }
    class Demo{
    public static void main (String []args){
    MyClass c=new MyClass();
    System.out.print(c.x+" ");
    System.out.print(c.y+" ");
    }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Please help to understand whats the wrong with my guess. - Simple program

    The problem is due to an innocent looking "void" misplaced in your code. Once you think on the fact that constructors have no return type, you'll quickly see the error in your program and why its behavior is what it is.

Similar Threads

  1. Simple sorting program, can't figure out whats wrong with my code
    By USC CSCE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 29th, 2012, 08:38 AM
  2. Can't seem to understand whats wrong with my code! Help!!
    By aquir in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2011, 01:06 PM
  3. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 09:33 PM
  4. What's wrong with simple Scanner program?
    By SV25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 21st, 2011, 08:05 AM
  5. whats wrong with my program??
    By jrodriguo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2010, 07:16 AM