Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Understanding static import and method overloading.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is why does the m() method only compile and run when it is not defined in run class? Why doesn't overloading for main and m occur? I also understand that adding import c.x.a; a.m(7); a.main("0","1"); works.


 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not too sure what you are trying to do with code. Anyways I've made it working code



Now it should compile correctly...but I'm unsure which class should be called when starting the program.
If I wanted to started this via command line what should I use



I may be best not to have too many methods with

public static void main(String[] args) {


at least when starting out. I also question the calls to many calls to "main" that you have. You seem to be making this more difficult that it needs to be. I would suggest refactoring your code to use less main methods as method has a special meaning when done this way

public static void main(String[] args) {

 
harsh asnani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand your concern regarding the use of main method. Fortunately, that was not the point. The code simply questions the validity given to methods in a scenario where the called methods exists in the calling class and when the called method is/are statically imported.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully I can help you out with some more info, which I believe to true.
Explicitly importing a class take precedence. Meaning:

is more important then

Also note you can call a static method by the class name as listed below:


If you explicitly call the method it takes precedence:



Given that static methods and variables belong to class definitions and not object references the only way you can overwrite a static methods or variables is with another static methods and static variables.
static methods and variables are also known as class methods and variables.
Methods and variables not defined in methods, but are defined in a class, are known as instance methods and variables. The only way to overwrite instance methods and variables is with other instance methods and variables.

Here is something else that may throw you for a loop is the following methods are not the same, but are 100% legal:

The two TestMethods listed above don't have to relate to each other and can do totally different things.
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
opps,

should be
 
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:opps...


I also noticed that this does not compile since class is not a valid identifier (but I understand what you mean)  
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably correct @Daniel Cox. I'm glad you understand what I that trying to get the point across on how things can be done differently. I'm also glad you were able to double check the code.

There may be a typo or else something a bit off as well. I did not test the code before posting it and I'm not as seasoned at Java as some of the people on this forum.

On a related note: If you are using an IDE like IntelliJ or Eclipse then the IDE will usually help you narrow down why something isn't working as expected. Don't get me wrong there is defiantly some things that you should know how to do using the command line/console. If you are studying/preparing for the the OCA 808 exam then you should do everything via console.
 
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

harsh asnani wrote:My question is why does the m() method only compile and run when it is not defined in run class?


When posting code you should always make sure that you post valid code which compiles successfully (unless you are of course asking a question about a compiler error). The statement main({"0"}); will never compile as it has an illegal/wrong creation of an array.

Please note that String... args (var-args) is syntactical sugar for String[] args. Meaning void main(String... args) is not an overloaded version of void main(String[] args), in fact it's the same method signature. So if you try adding them to the same class, you'll get a compiler error complaining about duplicate methods.If you change for example the type of the array, you'll have overloaded methods
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry @Roel De Nijs, I'll do better next time.

That being said, the reason that I did orginally answered this post was that I see none else had. I know that I'm no expert on any matters regarding Java or programming for that matter. However I thought that perhaps if I did post something to this thread that it may catch the eye of someone smarter and a discussion would ensue. Aside from that I thought that I could maybe learn something from someone else's post, if someone were to post that is.
And lastly, I know that sometimes it's helpful to know that your post was replied to, even if the reply is not 100% correct. It's nice to know that maybe you and whomever can work the problem and find a solution
 
Roel De Nijs
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

Pete Letkeman wrote:Sorry @Roel De Nijs, I'll do better next time.


Which post/statement makes you think I complained about one of your posts?
 
Pete Letkeman
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Roel De Nijs

"When posting code you should always make sure that you post valid code which compiles successfully (unless you are of course asking a question about a compiler error). "

I'm not 100% sure that the sample code that I provided would compile and have that you noticed this. As such I thought it would be wise to apologize for any problems that my code may have caused.
 
Roel De Nijs
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

Pete Letkeman wrote:I'm not 100% sure that the sample code that I provided would compile and have that you noticed this. As such I thought it would be wise to apologize for any problems that my code may have caused.


Ah ok! There were actually two small (but obvious) indications that it was not about your post First of all, I quoted a part of someone's post before I made my remark (and it was not your post, but the OP's). Secondly I mentioned which statement did not compile (main({"0"});) and that particular statement was not in the code snippet you've posted.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic