Results 1 to 6 of 6
  1. #1
    naved is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default whats wrong with my code

    i have created a constructor in my class and initialize variables inside the construct
    what i want is when i ll call a method and pass a argument while creating a object to constructor , the method should print my new values
    the code is


    Java Code:
    class test10  
    {  
        int a = 5;  
        int b;  
        public test10(int a , int b)  
        {  
            this.a = 10 ;  
            this.b = 50 ;  
            //System.out.println(a);  
            //System.out.println(b);  
            //System.out.println(this.a);  
            //System.out.println(this.b);  
        }     
        public void show()  
        {  
            System.out.println(this.b);  
            System.out.println(this.a);  
        }  
        public static void main(String[] args)  
        {  
            test10 t = new test10(56 , 80);  
            t.show();  
        }  
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,685
    Rep Power
    8

    Default

    What's your question? What's output are you expecting? What do you get instead? Have you stepped through this with a debugger to figure out what's going on?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    Try this...

    Im only learning myself but it compiles and runs ok. I think it was because you gave a and b values in the public test10. but i could be wrong so feel free to correct me

    Java Code:
    class test10  
    {  
        int a;  
        int b;  
        public test10(int x , int y)  
        {  
            a = x ;  
            b = y ;  
            //System.out.println(a);  
            //System.out.println(b);  
            //System.out.println(this.a);  
            //System.out.println(this.b);  
        }     
        public void show()  
        {  
            System.out.println(this.b);  
            System.out.println(this.a);  
        }  
        public static void main(String[] args)  
        {  
            test10 t = new test10(56 , 80);  
            t.show();  
        }  
    }
    Last edited by mrbrendano; 07-14-2011 at 08:36 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Please let the OP figure out his assignment himself.
    Spoon feeding code will not help a student learn how to write code.

    If you do post code, be sure to add comments describing what you have done, why you have done it and any extra considerations. In other words help the OP to fully understand your code.

  5. #5
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default

    my bad norm. wont happen again

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by naved View Post
    i have created a constructor in my class and initialize variables inside the construct
    what i want is when i ll call a method and pass a argument while creating a object to constructor , the method should print my new values
    Look at the constructor you have written. What happens to the two parameters that you pass in to it?

Similar Threads

  1. Whats wrong with my code
    By zit1343 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 01:06 AM
  2. whats wrong with this code
    By hamzeeco in forum Java Applets
    Replies: 1
    Last Post: 12-31-2010, 02:19 PM
  3. whats wrong with my code?
    By beandip408 in forum New To Java
    Replies: 6
    Last Post: 11-03-2010, 12:01 AM
  4. Whats wrong with this code???
    By javanew in forum New To Java
    Replies: 4
    Last Post: 03-28-2010, 05:46 PM
  5. Whats wrong with this code?
    By bbtgirl in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 03:51 AM

Posting Permissions

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