• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

when to call a method using object ?

 
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding was all instance method and variables should called through object reference variable. I was going through below example  and it confused me.



--------------------------------------

In method swim() floatInWater is called without reference at line 5 and flotInWater is not declared static in Bird class. In next method they created object of Swan and used it to call a method.
so please some one explain how it works ? what's the criteria to decide when to call method with reference? how do I decide on static method and instance method because here I don't find any static method.

Thanks in Advance:)
 
Ranch Hand
Posts: 116
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that code comes from the study guide from Sybex, by Jeanne Boyarsky and Scott Selikoff, right?

The thing is, all instance methods need an object to be used. In your example, you are calling the instance method floatInWater() inside another instance method swim(). And that's completely right.
The method floatInWater() is being inherited, because the class Swan extends from Bird, and the class Bird defines a floatInWater() method with a protected access modifier, so you can access this method directly inside another instance method of class Swan.
Now, think with me. When you create an instance of Swan outside the class Swan, you need to use it in order to call the instance methods from Swan.
For example:

Now, when you call the method swim() like in the code above, the method floatInWater() inside swim() will be called for the same object. Thus, an object is needed.

About the static methods, they don't need an object to be called. They belong to the class, not to the instance. And that's why you can't call an instance variable or instance method directly inside a static context (like a static method or static initializer)
 
Vidya Shivram
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jaoa.

working on few programs I inferred below points for quick reference can you confirm if they are right

1)Instance Methods can be called from another Instance method within a same class  without reference variable.
say

2)Instance Methods can be called from another Instance method in child class without reference variable.
3)Instance Methods cannot  be called from another Instance method which is outside the class without using reference variable.Here reference variable is used.
4)Static method cannot call Instance method without reference variable  even within same class or different class.
 
João Victor Gomes
Ranch Hand
Posts: 116
11
Hibernate Netbeans IDE Eclipse IDE Postgres Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just changing your code in order to compile


Chivid Ram wrote:
1)Instance Methods can be called from another Instance method within a same class  without reference variable.



Instance methods can be called within another instance method in the same class directly, but don't get confused about reference variables and objects. The reference variable itself isn't sufficient for calling an instance method. The reference variable needs to point to an object in memory in order to call an instance member without problems at runtime.

Chivid Ram wrote:
2)Instance Methods can be called from another Instance method in child class without reference variable.



Again, be careful with reference variables and objects, as I mentioned above.
About your points:
2 - You can call an inherited method inside another instance method that belongs to the subclass, but be careful with access modifiers. Let's take a look at some examples:


Now, what happens if the two classes are in different files and different packages? Let's see a different approach

But what if the tune() method of StringedInstrument was defined with default access (package private)? Then the code wouldn't compile, because a member with default access is only visible for the class where it was defined or for classes inside the same package of the class that the member belongs to. But in the example above, the classes are in different packages, and a member with default access inside the superclass StringedInstrument wouldn't be visible to the subclass ElectricGuitar.
However, the tune() method is defined with protected access in the previous example, which is fine, because the member is visible for classes in the same package or for subclasses (in any package).

Chivid Ram wrote:
3)Instance Methods cannot  be called from another Instance method which is outside the class without using reference variable.Here reference variable is used.



You can't call an instance method of one class in a method of the other class directly. So, you have to create an instance of it (again, not just the reference variable, you need to have an object).

Chivid Ram wrote:
4)Static method cannot call Instance method without reference variable  even within same class or different class.


Yes, static methods can't call instance members (methods or variables) directly (without an instance of the class), because a static method doesn't need an instance to be used, and we have to create an instance of the class (not just the variable) to call the instance member inside the static method.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to make it even more complicated: you can call a static method using a reference variable of that class (and you even don't need an actual object). So the following code snippet compiles and runs successfully (no exceptions are thrown at runtime )
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!