• Post Reply Bookmark Topic Watch Topic
  • New Topic

Shadowing  RSS feed

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shadowing.java:5: error: method m1 in class Shadowing.A cannot be applied to given types;
m1(1); //1
^
required: no arguments
found: int
reason: actual and formal argument lists differ in length
1 error

m1(1) is a method call to enclosing class ,why there is problem with it ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently Java does not automatically look in the enclosing class for a method that matches.

Determining which method needs to be called can be complex, and all the rules are described in detail in section 15.12 of the Java Language Specification. If you want, you can read that and find out how it works exactly.

You can make the program work by changing line 5 to this:

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

It seems unfair JABA
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:It seems unfair JABA

Why? Those are just the rules for how Java works exactly. Do you find it "unfair" just because it works differently than what you expected?
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
meeta gaur wrote:It seems unfair JABA

Why? Those are just the rules for how Java works exactly. Do you find it "unfair" just because it works differently than what you expected?


unfair because

an overloading scenario

public m1(){}
public m1(int i){}
public static void main(String[] args){
new obj().m1(1);
}
here compiler knows which one to call.


but in my program compiler doesn't understand that m1(1) is method call to m1(int i) not m1()
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would not be wise to compare one functionality with another.
Moreover in your code, you are not only trying to confuse the compiler, but also anyone else who tries to figure out what the code is intended to do.
Writing code that humans can understand beyond doubt is a skill
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Ghorpade wrote:It would not be wise to compare one functionality with another.
Moreover in your code, you are not only trying to confuse the compiler, but also anyone else who tries to figure out what the code is intended to do.
Writing code that humans can understand beyond doubt is a skill


Sorry, i will write better code next time ,i made compiler blind
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!