• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Features of Java I didn't know about

 
Ranch Hand
Posts: 957
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was looking at other folks Java code and saw features that I don't know about.   The code was flying by quickly so I didn't see all of the details.  I think it is common knowledge among  Java developers.  I'm not sure.  OK here goes:

A) I know of using @ as annotations in Java/selenium/TestNG but if it is java without the QA part what do the @ signs mean at the start of a line of code?

B) What does New at the start of a line mean?    It was always inside of a method of a class.  It looked like:


C) A try catch block with one try and multiple catches.  How does the block know which catch to use?

D) What does it mean when   is in a method on a line by itself?   I figure it is looking at something in it's calling class but what does it do?

Thank you in advance,

Kevin

 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A.
Annotations have a range of uses. On of the most common is @Override which indicates that a method is overriding a method in a parent class or interface.

B.

is sometimes used when you need to call only one non-static method of a class, but don't care about keeping the object around afterwards.
In my opinion this often shows poor design. It may be aproblem with the class being used - the method should be static or there should me more reason to keep the object around. It may also be a clue to the calling code not caring about object creation overhead, and not making use of the power, flexibility, and testability of dependency injection.

C.
Each catch block will only be used if the corresponding exception type is thrown. Think  of it a bit like a case statement but which switches on the type of the exception.

D.
super() on its own just means call the no-argument constructor of the parent class. This can only be done in the constructor of a child class, and is actually the default behaviour, so is not usually needed, but it is often added by tools which auto-create constructor code.

I hope some of that has helped!
 
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Frank Carver wrote:B.

is sometimes used when you need to call only one non-static method of a class, but don't care about keeping the object around afterwards.
In my opinion this often shows poor design. It may be aproblem with the class being used - the method should be static or there should me more reason to keep the object around. It may also be a clue to the calling code not caring about object creation overhead, and not making use of the power, flexibility, and testability of dependency injection.


A situation that this is often found in is to have the main() method create an instance of the  class and run one of its methods so that you're not forced to declare all the methods as static.
 
Marshal
Posts: 80099
413
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Frank Carver wrote:. . . this often shows poor design. . . .

It may be appropriate in a main() method invocation, where you only need a reference to the object running the app once.
 
Saloon Keeper
Posts: 28408
210
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
In the C programming language, there were pre-processor directives. They were used primarily to include header files, define manifest constants and serve as code macros. Although in some cases you'd find a directive like "#pragma" that was a (non-portable) directive to the compiler. Pre-processor directives always started with a "#".

Java made an early and conscious decision not to go the pre-processor route. However, they did want to be able to do magical things that weren't  directly related to the Java code itself. Accordingly, they selected the "@" as the magic indicator. There were probably 2 reasons why "#" wasn't used. First, just in case they wanted to change their minds later (or to use the C pre-processor, which can be excuted on any random text, not only for C code compilation). Secondly, because Java was developed under Unix, where some system resources such as shell scripts used "#" as a comment-to-end-of-line marker, just like "//" does for Java, C++ and C.

The first, and one of the most important uses of "@" was to flag text within comments so that the Java autoDoc processor could create more intelligent documentation. Automated documentation pre-dates Java (the first API auto-docs I ever saw were in 1986 for the Commodore Amiga), but it's a very important and useful practice in Java. Look at code samples I post here on the ranch, and probably most of them include JavaDoc comments.

As of Java 5, however, the "@" was expanded for more general usage. It has proven invaluable to supply meta-data for both compiling and runtime. Prior to that, a lot of Java frameworks had to parallel their classes with an XML config file. Annotations allow supplying config information within the Java source. XML config is not used mostly to override the annotations where you want to take a generic code resource and re-use it in a different context. Annotations serve both in core Java and JEE subsytems such as JPA and serialization (XML, JSON, [i]etc.[/t]) and third-party subsystems like the Spring Framework.


The new keyword is a monadic operator just like "!" is. Except that it's basically an invocations of the Java Class newInstance() method (note, this has changed recently). You sometimes see it inside code methods for 2 reasons: first, because in reality ALL code in Java is contained in either a class or member method, even the free initializers (an invisible "init" method is created to hold the logic). But that applies both to "new" instantiations that are then bound to some other object and  object that are used and immediately discarded like what you were talking about.

The reason for using new in such cases is that unlike C++, you cannot create an object by simply defining it, you always have to have it instantiated by active logic. That is, either operator new or invokind some method that constructs and returns the object. In C++, you can construct an object on the stack or in static memory, like so:

The object of class Point is created on the stack. A "Point *p = new Point();" on the other hand, would construct the Point on the program heap.

Java doesn't create objects on the stack. So only "new" works. Also note that in Java, object variables are references, not instances or pointers to instances. So this code:
does not construct a Point, it allots a reference to a Point. Unless you assign an instance of Point to it, "p" won't be a Point, it will be null.

Thus, if you need a Point for short-term calculations, do a "new Point()" in the method code and when the method exists, the Point will go out of scope and be subject to garbage collection.
 
kevin Abel
Ranch Hand
Posts: 957
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank, Carey, Campbell, Tim,
I appreciate the detailed responses.

I am doing QA manual work now but I get to see the Developers code during discussions.  
When I see something new I like to know what they are doing.

B) is the one that I don't completely follow.  I think I forgot what non-static means.  Is that the kind of method where you have to first set it to a variable before you may call it?   Is it like when I see
String x = new methodName();

new methodName();   calls the method but does not use memory to save the object in a variable.  The method is taken out of memory by garbage collection because nothing refers to it any longer.  

I'm not sure I'm making sense.  

B)

new SomeClass().someMethod();

is sometimes used when you need to call only one non-static method of a class, but don't care about keeping the object around afterwards.
In my opinion this often shows poor design. It may be aproblem with the class being used - the method should be static or there should me more reason to keep the object around. It may also be a clue to the calling code not caring about object creation overhead, and not making use of the power, flexibility, and testability of dependency injection.

Tnx,

Kevin
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a class, member variables (a.k.a. "fields") can be static or non-static. When a class is loaded an area is set aside in memory to hold a single copy of any static fields defined, ONLY ONE COPY. Non-static, or instance variables are allocated in a different region of memory and there's a copy FOR EACH INSTANCE that has been created by the running program (which may be zero instances). A static method may ONLY access static member variables. It may not access non-static fields. Non-static methods may access BOTH static and non-static member variables.

String x = new methodName();

You'd NEVER do a 'new' on a method name, only on class or type names.


is sometimes used when you need to call only one non-static method of a class, but don't care about keeping the object around afterwards.
In my opinion this often shows poor design. It may be aproblem with the class being used - the method should be static or there should me more reason to keep the object around. It may also be a clue to the calling code not caring about object creation overhead, and not making use of the power, flexibility, and testability of dependency injection.


I don't feel this statement fits all use-cases. There a cases where you create an object which then goes off to handle its own life-cycle and nothing else needs to retain a reference to control it, See my main() example.
 
Tim Holloway
Saloon Keeper
Posts: 28408
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It probably didn't help that I used "static" in a non-Java meaning for my C examples. In Java and in C++ the static keyword defines class-scope properties and methods. When I said "static" definitions of objects in C regarding instances, I meant instances that are defined as free-standing variables, not in function or method code. Addendum: or in class definitions!
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would add one thing to Frank Carver's comment part C..

the exceptions will always be handled by the the catch that it comes to first that will handle the exception.  that is to say, if you put a general catch first of say "Exception", and list specific types of exceptions in catch clauses after--the other catch clauses will never be hit because they are all children of the Exception class.

Les

Frank Carver wrote:
C.
Each catch block will only be used if the corresponding exception type is thrown. Think  of it a bit like a case statement but which switches on the type of the exception.

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

Les Morgan wrote:. . . the other catch clauses will never be hit . . .

The compiler will catch them and the code will fail to compile.
 
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I don't feel this statement fits all use-cases. There a cases where you create an object which then goes off to handle its own life-cycle and nothing else needs to retain a reference to control it, See my main() example.


Frank's point is that the stack frame that instantiates the service should almost never be the same stack frame that calls a method on that service. Either you instantiate a service and pass that service deeper into the application, or you call a method on the service, in which case the service should be injected as a constructor or method argument.

The main() method is a great counter-example, but that's because the main() method doesn't have any stack frame above it to create services for it to use.
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations kevin Abel,

Your question has made it to our Journal

Have a Cow!
 
kevin Abel
Ranch Hand
Posts: 957
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fritz,

I'm reading through some of 800 posts I started because at the time people explained things to me, I was in a rush or anxious to get something on a project working.   I am relaxed and reading through them at my own pace. I was reading through this thread.   I used C way back in time and don't remember the # or other notations so I got lost and stopped when I got a general ideas.

I never thanked you for the cow.    So, thanks.

Also, I didn't know about the Code ranch journal.

I just started reading a book that has a name something like learn git during your lunch breaks.   I'm only 11% of the way through the electronic version of the book.  If the book continues to be good, I'd like to write a review.  So far, the examples at the start of the book are not matching what I see when I use the GIT GUI.   I do like the fun part of how the author says "welcome back to lunch time" on occasion.  

Thanks,

Kevin

 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do the examples use commands or the GUI?

In general, I don't recommend using the GUI until you're very familiar with the commands. When you inevitably mess up something in your branches, you'll want to know how to fix it on the command line prompt.
 
Tim Holloway
Saloon Keeper
Posts: 28408
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that's the git book I think it is, I have a copy and the author not only provided some very useful answers when it was a promotion on the Ranch, but actually stayed around afterwards. Which is something few authors do, alas. Though I can think of one or two who are senior staff members here!

 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic