Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static methods

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If any one can help for this
class Test{
static int a = 0;
public static int abc(int x)
{
a=x+54;
return a;
}
public static void main(String arg[]) {
int s= abc(42);
System.out.println(s);
}
}
In the above class if I use the static method directly like
int s= abc(42);
it compiles and run fine..but why I can't use the methods from Math class like this , I mean without using the class name ..like
double a= sqrt(81);
Why I have to write
like
double a= Math.sqrt(81);
as sqrt is also public and static..
I will be thankfull if any one reply
Wali
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wali,
When you write,
double a= Math.sqrt(81);
you are referring to the sqrt method by giving a full path so that there is no ambiguity.
Regards,
Milind
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding something with what Milind has said..
the abc method you are calling from the same class hence no need to give a class name.. and Math's method we call from outside the Math class so we have to specify the class name and method name.
- Keyur
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing - Math cannot be instantiated, so you must use the Math name first. In the Test code, you can reach the static abc() internally (within the same source file) as
abx(x);
Test.abc(x);
or
Test t = new Test();
t.abc(x);
and externally (from outside the source file) as:
Test.abc(x);
or
Test t = new Test();
t.abc(x);
{assuming that x is an integer defined in both cases!}
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Milind , Shaha and Java Nut ,Now I am clear about it
Wali
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic