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 5 of 5

Thread: Static method

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Static method

    Hi!
    I put this small code here..!,I want to know,why always taking int argument passed method 1ly its taking?....

    how do i improve my java coding level,when i attending written test for an interview,i am failed 1st round itself......


    class staticTest1
    {
    	public static void test(double c){
    		System.out.println("Double");
    	}
    	public static void test(int b){
    		System.out.println("Int");
    	}
    		public static void test(short b){
    		System.out.println("short");
    	}
     
     
    	public static void main(String[] args) 
    	{
    		test(102420000000);
    	}
    }


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Static method

    In that specific code you will get a compile time error because that number is too big to fit in an int.

    However is that number was smaller and would fit into an int it would pick the int method over the other two because Java will always pick the method signature nearest to what you are passing in.

    // Json

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Static method

    Thanks,

    But there is also double value argument is there right?...so when i passing the value more than int value,it should automatically call the double value argument method right?...

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Static method

    Unfortunately, no. If you called it like this:

    test(102420000000[b].[/b]);

    It will figure out that you want a double and call the appropriate method.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Static method

    hey wait a second..

    i tried this one in the main method

    	public static void main(String[] args)
    	{
    		test(102420000000L);// i put an L , so it supposed to be "LONG" integer ryt?
    	}


    but it refered to the method signature that has 'double' argument

    it displayed .

    Double


    and how am i supposed to refer at the method with 'short' argument?
    what kind of number should i pass to a method 'test' to refer it to the method with 'shor' parameters?
    what number format?
    it always display 'int' instead of short.



    why?
    Last edited by chronoz13; November 20th, 2009 at 11:12 AM.

Similar Threads

  1. Help with CoinCounter method
    By abielm_007 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 3rd, 2010, 01:19 PM
  2. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  3. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 04:37 AM
  4. [SOLVED] Method declaration in Java
    By mohsendeveloper in forum Object Oriented Programming
    Replies: 4
    Last Post: June 11th, 2009, 03:18 AM