Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Unclear Concepts

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some easy questions from the jqplus software, but need to clarify some basic java principle concepts
QUESTION 1
Given the following set of member declarations, which of the following is true?
int a; // (1)
static int a; // (2)
int f( ) { return a; } // (3)
static int f( ) { return a; } // (4)
***************************************************************
Ans 1 and 4, cannot occur in the same class
1 and 2 cannot occur in the same class
***************************************************************
Question? what about 2 and 3, ie a static variable passed
to a non static method ? Wouldn't that give a compile
time error? If if doesn't, then in what situation do
we have a compile time error when a static variable is
passed to a non static or vice versa?
***************************************************************
QUESTION 2
What will be printed when the following program is compiled and run?
class Super
{
public int getNumber( int a)
{
return 2;
}
}
public class SubClass extends Super
{
public int getNumber( int a, char ch)
{
return 4;
}
public static void main(String[] args)
{
System.out.println( new SubClass().getNumber(4) );
}
}
What will be printed?
***************************************************************
ans 2,
***************************************************************
Question: correct me if I am wrong but isn't the getNumber() overloaded? If not, what is the logic behind the answer to this question?
Also, assuming the getNumber()was overloaded, will it call its immediate class or the superclass of the getNumber() method
***************************************************************
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mo,
Lets take the first one first.
Here you can not declare two instance variables with the same name , in this case a it does not matter whether they are static or not. and the same goes for the method declarations too, the fact that one is static does not matter, you are trying to override the method f() in the same class which is not allowed, whereas overloading is allowed so the following will work.
//int a;
static int a;
int f(int a) { return a; }
static int f( ) { return a; }
Coming to the second problem since java practices run time polymorphism, the object checks the signature of the method calleat runtime and calls the method which satisfies that signature in this case found in its super class.
class Super
{
public int getNumber( int a)
{
return 2;
}
}
public class SubClass extends Super
{
public int getNumber( int a, char ch)
{
return 4;
}
public int getNumber( int a)
{
return 444;
}
public static void main(String[] args)
{
System.out.println( new SubClass().getNumber(4) );
}
}
in the above case the subclass version of the method is called. Hope it helps.
Bhaskar

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Passing static variables to non-static methods is fine:

There is no problem with this.
How about:

Again, no problem.
The only problem is when a static method tries to access an instance variable. This for example, will cause a compile time error:

 
mo odunaiya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply,
the concept is clear to me,......i think !!!
To verify what you said, in a nutshell say:
public class Mohammed {
int a
static methodman(int b){
system.out.println("compile error" +a); //this won't compile
}
}
******************
******************
public class Mohammed {
int a
methodman(int b){
system.out.println("compile success" +a); //this will compile
}
}
************************
************************
public class Mohammed {
int a
static g
public static methodman(int b){
system.out.println("compile success" +g); //this will compile
}
}
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your second problem the concept is method overloading. In the derived class
the method "int getNumber"
exists with two distinct signatures

They are

public int getNumber( int a)
{ return 2; }

public int getNumber( int a, char ch)
{ return 4; }
Now the O/p will be depending on the parameter u have passed to that method.
When you pass int getNumber(4) ====> o/p will be 2
and when u pass int getNumber(65, c) ====> o/p will be 4 (where c is a char)
HTH
 
mo odunaiya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response, it does make sense now!
What an easy concept! .....all I needed was to have the "wool taken off from my eyes" to see!!
Can you hed more light on the response I made for Thomas Paul the bartender, on the issue of passing a static variable to a non-static method. Does my code make sense?
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mo odunaiya:
Can you hed more light on the response I made for Thomas Paul the bartender, on the issue of passing a static variable to a non-static method. Does my code make sense?

Yes, you have it down perfect.

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic