• 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
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Methods

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I am new to java and I know the question that I am going to ask is kind of weird, however please help me as I am new to this.

This is the code: -



The problem is that when i try to call method m1() at line 1, it gives compilation error ("cannot resolve symbol m1() ") and when I call it inside any method, then it is fine.
So I wanted to ask that is it because if we call m1() in line 1 then it is of no significance as it cannot be executed  and if we call it inside method then we can execute it.

And one more thing I wanted to ask that : - Implementing interface does not means that class will inherit its static and default methods ?
Correct me If I am wrong about the above statement.

Please help me with this.
 
Master Rancher
Posts: 4460
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method call needs to be inside of a method.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Norm, Thank you for replying. I know the method call need to be inside method. However I want to know why it is so.
And why it gives error on line 1.
Is it because, that by calling method outside of any method, naked in the class, it will not be executed, so it is of no use ?
 
Norm Radder
Master Rancher
Posts: 4460
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will need to wait for someone with the rulebook to explain why.
 
Marshal
Posts: 73760
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because the grammar doesn't have statement as something that can be inside a class.
 
sahil Kairon
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell Ritchie, I have also searched it in JLS and have found that statement is not written in class body rules, so we can only do declaration in class body.

Once again thank you to all who replied to the query, I know it was quite silly, however I was not able to understand it, so I thought of asking it on the forum.
 
lowercase baba
Posts: 13001
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't consider any question as silly.  If you are confused by something, feel free to ask!!!  
 
Campbell Ritchie
Marshal
Posts: 73760
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahil Kairon wrote:Thank you . . .

That's a pleasure

. . . I know it was quite silly . . . .

Fred is correct; it is not silly, but quite a difficult question.
The javac tool is designed to follow the grammar and produce errors whenever the code doesn't fit the permitted grammar. Presumably, in the early days of the language it was decided that statements should only be permitted in side methods or similar. You wouldn't want statements outside methods, etc., because it is not defined how they would be called. Designers of languages have to make such decisions.
And well done finding that part of the JLS
 
Saloon Keeper
Posts: 24207
166
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Important note: It is a convention in Java that type definitions (such as class and interface names) should start with a capital letter. The language doesn't care, but some tools do and it can confuse people. In tact, I suspect it confused a couple of people here.

Speaking of confusing, while a statement like:
Could be an error, the statementIs 100% legal Java. There's an "invisible" method where all the instance initialization code gets put (and, if needed, another one for static initialization).

 
Campbell Ritchie
Marshal
Posts: 73760
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . .. . .

That is a declaration and initialisation which is counted as a special kind of statement and that is permissible outside methods. It creates a(n instance) field, so that it normal programming.
 
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And one more thing I wanted to ask that : - Implementing interface does not means that class will inherit its static and default methods ?
Correct me If I am wrong about the above statement.



Static and Default methods in interfaces are relatively new additions to Java, as of Java 8, but now arguably are part of "Beginning Java".

Historically, we thought of an Interface as containing only a contract of provided behavior, pure abstract methods, and no implementation.

This posed a serious problem, as no additional functionality could be added to an Interface already released "to the wild" without breaking all code that implemented the Interface.

Default methods allow Interfaces to contain some implementation/behavior.

Some people think of them as just dummy placeholders to allow existing implementations of the interface to continue compiling and running.

The Default methods are most definitely inherited by all classes implementing the Interface in question.

Additionally, in many cases the default methods provided are actually very useful.
One can override in a class that implements the interface if there is a reason to, and your overrides will be called just as if you had overridden a method from a superclass.
The Java class libraries now have many default methods on common interfaces that are very useful as they are, and can often be used without needing to override them.

Static methods can now also be placed in interfaces, and are often extremely useful.

They are NOT inherited, but anyone with access to the interface can call these methods by just saying
InterfaceName.staticMethodName()

(not just code in classes implementing such an interface)

Whether you should be designing interfaces as a beginner in Java that make use of static methods and default methods is a legitimate question, many would say "not yet" and others might say "Why not?"

It is important to understand how to make use of the VERY USEFUL default and static methods that many Java interfaces now provide, however.

They are all over the place and you can't make full use of the interfaces without getting comfortable with them.

I hope that begins to answer your very good question that I quoted and replied to here.
 
Jesse Silverman
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see you are currently studying for the OCA exam.

So the best answers to your questions about default and static methods on interfaces should probably consider what is in scope for the OCA exam.

I believe interfaces provided by Java that are in scope for that exam contain default and static methods that are important for you to know.

I don't recall whether how to implement them and the rules for their inheritance when implementing multiple interfaces that may each contain default methods are in scope for the OCA or not.

The slightly tricky rules about that are in scope for the OCJP, but I am not sure about the OCA.

It is all great stuff to know eventually, but maybe not yet.
 
Campbell Ritchie
Marshal
Posts: 73760
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . now arguably are part of "Beginning Java". . . .

I think they are just as much part of beginning Java┬« as heating water is part of making tea. Nothing “arguable” about it
 
reply
    Bookmark Topic Watch Topic
  • New Topic