Anonymous Class in Java

Anonymous classes can be tricky to understand for beginners.Mainly because the syntax can look verbose or at least it looked very confusing to me when I started my career as a java developer.So let’s talk anonymous classes today.

Anonymous classes were introduced in java to make code more concise and reduce the lines of code.Here’s some facts about anonymous classes

  • We can declare and instantiate an anonymous class at the same time.
  • They do not have a name.
  • An anonymous class have access to the members of its enclosing class.
  • An anonymous class can not access local variable in its enclosing scope that are not final or effectively final.
  • A variable declaration in an anonymous shadows any variable in its enclosing class with the same name
  • Can not declare static initialization blocks in anonymous classes.
  • Can not declare a constructor in anonymous classes

Example of anonymous class:-

Anonymous Class Example

When you run the program , you will get the following output:-

Syntax of Anonymous Classes:-

An anonymous class is an expression.It contains class definition inside the curly braces.

It has the following pieces:-

  • It starts with the new operator.
  • The name of the interface to implement or the class name.
  • Parenthesis that contain arguments to the constructor just the way we do it in a normal object instantiation.
  • A body of the anonymous class.The body starts and ends with curly braces and colon at the end of the ending braces

You can declare the following in anonymous classes:-

  • Extra Methods which are not originally declared in the interface or the class you are implementing
  • Initialization blocks
  • Fields
  • Local Classes

You can create an anonymous class from an interface or a class or an abstract class.

References :- Oracle Docs

That’s all about anonymous classes . I hope you enjoyed reading about anonymous classes.Have fun with coding.

How to Configure Java 1.8 On Windows

In this post I am going to explain how to install Java 1.8 on your machine and verify the same.

Here’s the steps:-

Install Java 1.8

You can install Java1.8 from Oracle website.Here’s the link:-

https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

Install Java 1.8

You will see there are two executable files for windows, one for 64 bit and another for 86 bit.Check you System specification by going to Control Panel -> System and Security ->System.

System Speciifications

You need to download the executable file and run it on your local machine.By default the java package will be installed under C -> Program Files->Java. But you can change the path as per your specifications.

Install Java

After your java package is installed you will get the following screen:-

Java Installation

Set Environment Variables

You need the specify the path to the java folder using the PATH variable under Environment variables of your machine. You can access the environment variables of your machine by right click on My Computer or This PC ->Properties

Set environment variables

Set the Variables

User Variables:- 

JAVA_HOME :- C:\Program Files\Java\jdk1.8.0_221

CLASS_PATH:- %JAVA_HOME%\lib;%JAVA_HOME%\jre\lib

set variables

Environment Variable

Add the following in the path variable:- 

PATH:- your-unique-entries;%JAVA_HOME%\bin

Verify Java Installation

Check if Java is correctly installed on your machine.Go to command prompt/terminal and type the following command:-
java -version

Verify Java Installation

Troubleshooting

If in the command prompt its shows that the java is not recognized as a command, revisit the variable and their paths.You will get this error when the user variable and environment variable paths do not point to the correct location of java folder.

Another reason might be that the Java package is not installed properly ,check your bin folder and jre folder.If required reinstall the java package and following the above steps again


How to fix build error related to Jacoco Test Coverage

Code coverage is a software metric used to measure how many lines of code is executed during automated tests.

Jacoco is a code coverage report generator for Java Projects. You need to add the maven plugin for jacoco and get the code coverage reports.

Jacoco Plugin dependency in pom.xml:-

Jacoco Plugin Dependency




You can also configure the code coverage ratio in the plugin by using the rules tag like this:-

Configuration and rule tag

You can either write the covered ratio as 10% or 0.10 .

Sometimes we get a build error like following 

Jacoco Build Error

To fix this issue simply add more test cases in the module which is showing the build failure.

For instance in the above stack trace the module (gcmms-api-client) is showing coverage ratio is 0.09, but expected minimum is 0.10 so simply add more test cases in this module and build the project again.The issue will be resolved.

6 Ways to deal with workplace bullying

Dealing with workplace bullies can be a tough nut to crack.Statistics show almost 60% people faced workplace bullies and harassment at their workplace.

Workplace bullying can be verbal or physical and it can be your co worker ,your manager or anyone else working with you who may or may not benefit from this type of act.In most cases it’s usually the person who is insecure or controlling ,tries to bring you down and gain from the situation for instance taking credit for your work or a promotion.

Here are 6 tips to deal with workplace bullies:-

 .

  • Know your rights 

 Read your company policy.Almost every company have a workplace harassment policy .Read it thoroughly  so that you know what steps you can take to get yourself out of this situation.

  • Document Evidences and Plan your move

Don’t be in a haste to take action.Take your time to analyse the situation and also the consequences.It can be daunting and you might be afraid of losing your job.List down all the steps you can take and also the consequences be it positive or negative.

  • Confrontation

Give your bully one last chance.Talk to the person who is trying to bully you and politely ask him to back off and stop this behaviour.Give him/her some time, maybe a week to three weeks and if it still doesn’t stop you already have a plan in place.

  • Communicate

Plan a meeting and talk to your supervisor and give him details about the bully and how it is impacting you.Be precise and clear. It’s better to come up with specific details rather than bad mouthing or criticising the bully .This way you will show up as mature and sensible .You don’t need to take things personally .Keep this professional with all the evidence and specific incidents related to the bully.If your manager itself is the bully talk to his manager about it.

  • Follow Up 

.After a few weeks if you feel no action has been taken and the behaviour continues , talk to the HR .After you have placed your case ,give some time to your supervisor may be a week and then follow up once or twice.If no action has been taken, talk to a level up the hierarchy 

  • Time Up 

 If you have taken all the above 5 steps and  the issue is not resolved its best to look for alternate options .Find a  new job as soon as possible where you are respected and heard.There is nothing worse than spending 8 hours working with a person who makes you feel worthless and no action is taken in this regard.Make yourself a priority and do yourself a favour by switching your job.

Let me know if any of you have faced workspace bullies how to dealt with it.I would love to hear it from you guys.