• Post Reply Bookmark Topic Watch Topic
  • New Topic

New statement without variable declaration  RSS feed

 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While studying for my OCAJP 8 exam, I encountered a code snippet in a practice question that looked something like:



I first thought that this code should not compile, because of the part, but I was surprised that it actually compiled correctly.
Is this a good practice to execute a method this way or was it just a way to trick me?

 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code looks familiar, did it by chance come from Jeanne Boyarsky and Scott Selikoff Java 8 OCA Study guide?

I'm not sure if this is good practice or not. I would not recommend it as it could be lost when reviewing/maintaining the code. That being said there are times when knowing how to do something like this could be handy and I'm sure that the test will have trickier questions then this.
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:This code looks familiar, did it by chance come from Jeanne Boyarsky and Scott Selikoff Java 8 OCA Study guide?


You are right, I modified it a bit, but the example comes from the Study guide.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works because the "new" has a higher precedence than the "." operator.
So it's the same as if you'd written
It's a good idea to become comfortable with this syntax because it exists in lots of production code.
 
Arco Brouwer
Ranch Hand
Posts: 44
2
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! The main thing I'm wondering about, is that this is only a statement to let the Lion roar and not to bind it to an variable. Since it compiles, it tells me that the syntax is correct, but why should you use a statement like this?

In the meanwhile I studied a little further, and wouldn't it be better to use a static method for this? In this way you can call the roar method directly from the class?
 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are reasons to use static methods, however there are also reasons not to. One should not assume that they know the original intention of the programmer unless it is well documented especially when coming back to the code weeks (or more) later.

I know for me, in the past, sometimes I started off trying to create a program with a specific goal in mind, but then after the client and/or manager changed their minds I had to change the goal. And when changing the goal I looked back at what I did and maybe I re-purposed a line or two of code.

For something small like these few lines of code, sure static would work. As to if it is better or not that depends on what else the program is doing. I'm sure that there are plenty of resource out there which go into depth on static methods vs instance methods and I don't thing that one rule applies to everything.

That is my take on this at least,
Pete
 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To follow up, items this posting are not anything a beginner should be concerned about.

On my Windows 10 system:
  • if I used static then the size of Lion.class is 472 bytes.
  • if I used version listed in this post then the size of Lion.class is 484 bytes.


  • If you choose to, you can use the tool javap https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javap.html to get a look under the hood and see what is in the class file.

    Again, this is not something a beginner needs to worry about, and it is NOT on the OCA 808 exam.
     
    Knute Snortum
    Sheriff
    Posts: 4279
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Arco Brouwer wrote:The main thing I'm wondering about, is that this is only a statement to let the Lion roar and not to bind it to an variable. Since it compiles, it tells me that the syntax is correct, but why should you use a statement like this?

    You could write
    but seasoned programmers will notice that you don't need the lion variable, so why create it?  That's why
    is a Java idiom.

    In the meanwhile I studied a little further, and wouldn't it be better to use a static method for this? In this way you can call the roar method directly from the class?

    For new programmers, the rule of thumb is, If there isn't a really good reason to make a member static, don't.  A good reason might be that you want to create a constant, like pi, if there wasn't already Math.PI.  A bad reason would be, I have all my code in main.  Note that in the source of Math.java, you see:
     
    Arco Brouwer
    Ranch Hand
    Posts: 44
    2
    IntelliJ IDE Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote: but seasoned programmers will notice that you don't need the lion variable, so why create it?  That's why

    Thank your for the replies.
    The reason I asked this question was for me to understand whether or not it is useful to use statements like this. From a beginners perspective I think I will stick to the Class newClass = new Class() statement.
    But since sentences like this occur a couple of times in the book and in some practice exams I use, I was a bit confused, but it is more clear to me now!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!