Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

method call

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai friends,
this is another question from the same paper maruce green2. the ans option is given as 3 but i answered it as 4 since there is no call for the amethod. plz clarify.

What will happen when you attempt to compile and run the following code with the command line "hello there"
public class Arg{
String[] MyArg;
public static void main(String argv[]){
MyArg=argv;
}
public void amethod(){
System.out.println(argv[1]);
}
}
1) Compile time error
2) Compilation and output of "hello"
3) Compilation and output of "there"
4) None of the above
thanking u
shyam
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shyam..
The answer should actually be 1) Compile time error. There are two errors actually in this piece of code
1) String[] MyArg;
public static void main(String argv[]){
MyArg=argv;
}
The non-static instance variable "MyArg", cannot be accessed from the static "main" method
2) public void amethod(){
System.out.println(argv[1]);
}
This method does not know anything about the local variable argv[] (local to
the main method) and another error is thrown.
Hope this makes it clear.
Thanks
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kumar
I agree with you. there should be no output, but shouldn't it cause a compilation error cause the String array MyArg is not static???
there is no object calling the method, and this does not let it print the there
I had to ammend the code to even get it to work.
Sorry Foo8 is my test file it never gets mixed up from my work
Davy
 
Sham Grandhe
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai Kelly & Ashwin,
ur explanations have made me clear I had not see about the non-static member being assigned.
thanks
shyam
 
get schwifty. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic