Results 1 to 20 of 20

Thread: static method

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138

    static method

    I am trying to learn java on my own and I need your help.

    I have the following question?

    package NAME
    public class NAME
    {
    int X;
    int Y;
    public static NAME CHANGE(INT A)
    {
    X=A;
    Y=A;
    return?????????
    }
    }
    I KNOW I HAVE TO RETURN SOMETHING BUT I DON'T KNOW WHAT
    thanks for the time

  2. #2
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    you can return anything, if your changing the name maybe you can just return "1" for successful

    also, you can just do

    Code:
    X=Y=A;
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138
    Nab

    But the method

    public static Name change(int x)
    is asking to return type Name which is the classname

    so what do I return
    1?

    Thanks for your time

  4. #4
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    change it to

    Code:
    public static void change(int x)

    and you dont have to return anything
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138
    Nab.

    the function needs to remain as is

    public static name(the name of the class) change(int x)
    what would you return, because I tried with 1 and it didnt work


    Thanks again

  6. #6
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    Maybe

    Code:
    return (NAME)1;
    what happens when you dont return anything?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    If the function needs to remain how it is(although I can't really tell what it is doing), you can just return null. But you will get a NullPointerException if you try to use the object returned from the function.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I don't know im pretty lit right now but it looks like he is naming the method the same name as the class and wouldn't that make the compiler consider to be a constructor? It looks like a constructor and if it is it should be in the form of public/private
    name(pramlist) throws{exceptions}

  9. #9
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    public static NAME CHANGE(INT A)
    I think the return type is NAME, and the function name is CHANGE.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  10. #10

  11. #11
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by Dilenger4
    Couldnt he return just return this ?
    * shrug *

    Like I said, I don't really follow what the function is doing.

    But come to think of it, static methods don't have access to this, do they?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  12. #12
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
    public class returntest{
      public static void main(String[] args){
       test t = test.returnme();
       
       
       System.out.println(t.teststring);
     }
    }
    class test{
       String teststring = "Hello"; 
       public static test returnme(){return new test();} 
    }

  13. #13
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by Dilenger4
    Code:
    public class returntest{
      public static void main(String[] args){
       test t = new test();
       System.out.println(t.teststring);
     }
    }
     class test{
        String teststring = "Hello"; 
        public test returnme(){return this;} 
    }
    Yeah, but returnme is not a static method,
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  14. #14

  15. #15
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    GUARO what is the need to have a static method that returns a refrence of it's enclosing class? Would't you want to go with using a constructor or are you being forced to use other specs?
    Code:
    public Change() {}

  16. #16
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by Dilenger4
    That code should work.
    I don't doubt that it will . But if you change it to a static method, it should error during compilation.

    (Look at me using the big words)

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  17. #17

  18. #18
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    wouldn't you do...
    Code:
    package NAME 
    public class NAME 
    { 
        int X; 
        int Y; 
        public static NAME CHANGE(INT A) 
        { 
            NAME myName = new NAME();
            myName.X=A; 
            myName.Y=A; 
            return myName;
        } 
    }
    I dont know why you would want to do that but that would fulfill your requirement.
    It creates a new instance of a NAME and assigns the two values X & Y the value of A.

    Oh and just in case you didn't know there are java naming conventions like Classes are all lower case apart from the first letter of every word. An article on Coding conventions can be found here... http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html
    Sorry if you knew that and were just putting them as capitals for emphasis.
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Posts
    138
    lady K

    Thanks it worked. I understand now.

    Thanks to all for your time, I really appreciated

  20. #20
    Member daveyboy's Avatar
    Join Date
    Feb 2002
    Location
    Aberystwyth, UK
    Posts
    33
    What is the point of having a static method which returns an instance of that classes, surely the following would be better?

    Code:
    public class Name
    {
        int X;
        int Y;
    
        public void change(int A)
        {
            X = A;
            Y = A;
        }
    
        public static void main(String [] args)
        {
            Name myName = new Name();
            myName.change(3);
        }
    }
    then the contents of the main method could go anywhere....


    Infinity isn't large it's just incomprehensible

Posting Permissions

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



Click Here to Expand Forum to Full Width