Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Post my java programe

    i created a programe and passed a value but didnt work why?
    public class mymethod {

    int myint;



    void method1(int value){
    value=myint;
    method2(" sri lanka" );

    System.out.println(value);
    }

    void method2(String myst){



    }
    }


    public class mymain {
    public static void main (String[]args){
    mymethod obj=new mymethod();
    obj.method1(5100);
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, seeing as how that program does not do anything ...

    However, describe "does not work", that is the singularly most useless problem description in the world.

    However, why do you set the local passed value to the undeclared instance variable value (instead of the other way around), and then do nothing with it, and then call another method with a hard-coded string (and that method is empty). What was the argument for, either time?
    Last edited by masijade; 12-09-2010 at 12:58 PM.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Please use CODE tags.

    Java Code:
    public class [COLOR="Blue"]MyMethod[/COLOR] {
    Might as well learn good style when you're just starting out, rather than develop bad habits. Class names should start with a capital letter, and if they're two words, capitalize the subsequent ones.
    Java Code:
        int myint;
    This is an instance variable, and when you don't specify otherwise, it's package-protected. Get in the habit of making your instance variables private, until you have a good reason to do otherwise. It's better for encapsulation. Other classes should interact with your class via its public API, not by messing around directly with its state.
    Java Code:
        void method1(int value) {
            value = myint;
    Your method was passed the parameter value, but you've overwritten it with whatever was in your instance variable myint -- which is nothing at all. I think maybe you intended to do that the other way around.
    Java Code:
            method2(" sri lanka" );
    
            System.out.println(value);
        }
    
        void method2(String myst){
    
        }
    Did you mean to do something there?
    }
    Java Code:
    public class [COLOR="Blue"]MyMain[/COLOR] {
        public static void main (String[] args) {
            [COLOR="Blue"]MyMethod[/COLOR] obj = new [COLOR="Blue"]MyMethod[/COLOR]();
            obj.method1(5100);
        }
    }
    This is legal, but you really don't need a separate class here. This would work just as well:
    Java Code:
        public static void main(String[] args) { // still in MyMethod class
            MyMethod obj = new MyMethod();
            obj.method1(5100);
            // or even just this:
            (new MyMethod()).method(5100);
        }
    }
    -Gary-

  4. #4
    Bertstar is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Quote Originally Posted by vibaviattigala View Post
    i created a programe and passed a value but didnt work why?
    public class mymethod {

    int myint;



    void method1(int value){
    value=myint;
    method2(" sri lanka" );

    System.out.println(value);
    }

    void method2(String myst){



    }
    }


    public class mymain {
    public static void main (String[]args){
    mymethod obj=new mymethod();
    obj.method1(5100);
    }
    }
    Not sure if srs......

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    :headdesk:

  6. #6
    1060406004020 is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Smile what result do you want to get?5100?

    if your intent is 5100,then you will disappoint.
    because the default value of myint is 0.No matter what you give to value, expression "value=myint;" will make value=0;So the print result will bo 0,not 5100.

Posting Permissions

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