Sat Dec 29 03:02:08 PST 2018


"Can a non-static method access a static variable or call a static method" is one of the frequently asked questions on
static modifier in Java, the answer is,
Yes, a non-static method can access a static variable or call a static method in Java. There is no problem with that because of static members i.e. both static variable and static methods belongs to a class and can be called from anywhere, depending upon their access modifier. For example, if a static variable is
private then it can only be accessed from the class itself, but you can access a public static variable from anywhere. Similarly, a private static method can be called from a non-static method of the same class but a public static method e.g.
main() can be called from anywhere.








How to remove all unused imports in Eclipse


Eclipse IDE gives warning
"The import XXX is never used" whenever it detects
unused import in a Java source file and shows a yellow underline. Though unused import in Java file
does not create any harm, it's unnecessary increase length and size of Java source file and if you have too many unused imports in your Java source file, those yellow underline and Eclipse warning affect readability and working. In my last post on Eclipse, we have seen some
Java debugging tips on Eclipse and in this post, we will see Eclipse shortcut to remove all unused imports in Eclipse. There are many options to tackle this problem e.g. you can collapse import section of code in Eclipse or you can altogether remove all unused imports from Java file, and we'll see them in this short tutorial.








Even though almost all Java programmers either use
JUnit or
TestNG for there unit testing need along with some mock object generation libraries e.g.
Mockito, but not everyone spends time and effort to learn subtle details of these testing libraries, at least not in proportion of any popular framework like 
Spring or Hibernate. In this blog post, I am sharing one of such detail, which has puzzled me a couple of years ago. At that time, though I had been using
JUnit for a significant time, I wasn't aware that code written inside the constructor of Test class is executed before each test method.  This behavior of
JUnit has caused, some of my test to failed and putting hours of investigation in my code, without realizing that this is happening because of JUnit is initializing the object by calling constructor before executing test method annotated with
@Test annotation.







Comments

Popular posts from this blog

termux vnc viewer setup

../Settings.jpg

me.html