Showing posts with label access. Show all posts
Showing posts with label access. Show all posts
Thursday, August 10, 2017
Proper way to access file resources in junit tests
Proper way to access file resources in junit tests
As I was refactoring some JUnit tests recently I was reminded of an important fact on the proper way to read in a file.
In maven any file under
/src/test/resources is automatically copied over to /target/test-classes. So for example lets say I need to read in a wsdl in my test. So I place the wsdl in the resources folder: /src/test/resources/test.wsdl. Now at first it might be tempting to reference the file with the following code:
File testWsdl = new File("/src/test/resources/test.wsdl");
and this will actually work on a windows machine; but what about linux, cygwin, or even solaris. Your next improvement might be to replace the
/ with File.separator:
File testWsdl = new File(File.separator + "src" + File.separator + "test"
+ File.separator + "resources" + File.separator + "test.wsdl");
This still isnt the best solution which is presented below:
URL url = this.getClass().getResource("/test.wsdl");
File testWsdl = new File(url.getFile());
Using the classs resource will locate the file in the tests classpath, which happens to be
/target/test-classes. And this final solution will work on windows, linux, cygwin, and solaris.Available link for download
Sunday, July 9, 2017
Public access modifier in java
Public access modifier in java
As its name public itself indicates that any member of the class declared as public will be visible and accessible ANYWHERE. We have two scope in java programs :
1. Class scope
- Sub-class in the same package --> Public is visible .
- Sub-class in different package --> Public is visible.
- Another class in the same package --> Public is visible.
- Same class in the same package --> Public is visible.
2. Package Scope
- Within the same package -- > Public is visible.
- Outside the package ---> Public is visible.
So public members are visible EVERYWHERE and can be accessible form ANYWHERE.
Where and how we can use public access modifier in java ?
--> Public can be used with class, interface, constructor and variables.
Public with class
If we are not using any access modifier with the class name then by default it is declared as default and declaring default will restrict its accessibility to the same package only. So to make any class visible to the another package we have to use public with the class. For ex-
public class prog { }The above class is now visible to the other packages as well.
Public with constructor
Public with variable
A.java in com package
------------------------------
By default constructor is using default keyword before its name whose visibility will be limited to that package only in which class is been defined. If we want to access that constructor through another class which is defined in another package then we need to define the constructor as public to have its visibility to the other packages as well in which that is been imported. For ex-
Let suppose there are two class A and B in different packages com and def respectively and we import class A using import statement in def package where class B is defined to access the class A constructor by creating the object of class A. Now we came across few cases :
1. EXPLICIT CONSTRUCTOR : - If in com package any explicit constructor is there which is by default default and then if we try to create class A object in class B then it will compilation error that " constructor is not visible ".
2. NO EXPLICIT CONSTRUCTOR :- If we are not defining any constructor explicitly then JVM will invoke by default constructor and place public keyword before the constructor and hence it wont give any compilation error.
3. PUBLIC EXPLICIT CONSTRUCTOR :- And if we are declaring constructor explicitly as public then it work fine and wont give and any compilation error.
Public with variable
Like constructor we can also use public keyword with class variable but not with the local variables as their scope will be within the method or constructor block only. Non-public members give compilation error " not visible ". Defining public with the class variables means that it can be accessible in the class which is defined in other package. for ex-
Let suppose there are two class A and B in different packages com and def respectively and we import class A using import statement in def package where class B is defined to access the class A variables by creating the object of class A. like -
------------------------------
package com;
public class A {
public int z=30; // public variable.
int y=20; // Be default default variable.
public A(){
System.out.println(y);
System.out.println(z);
}
public static void main(String sush[]) { }
}------------------------------
package def;
import com.A;
public class B{
public static void main(String sush[]) {
B b = new B(); // Class B object.
A a = new A(); // Class A object.
System.out.println(a.z); //compiler successfully as it is
declared public in class A.
System.out.println(a.y); // As y is not public so
compilation error.
}
}
Public with interfaces
Using public keyword before the interface will make it visible to any classes in any packages. If we are not using any public then it will not be visible to other packages and hence no class can be able to implement that interface. Without using public to the interface if we try to import that that interface it will give compilation error as " not visible " . for ex-
Let suppose there are two java files A and B in different packages com and def respectively where are A.java contain an interface and B.java contain a java class B. We are importing interface A using import statement in def package where class B is defined to implement that interface using implements keyword. for ex-
A.java in com package
------------------------------
package com;
public interface A{
void display(); // It is By default public and abstract.
}
B.java in def package
------------------------------
package def;
import com.A; //No compilation error as interface is public
public class B implements A{
public void display(){
System.out.println("Display() method");
}
public static void main(String sush[]) {
B b = new B();
b.display();
}
}The above program compile and execute successfully as because we are importing public interface A.If we are not using any public keyword then it will give compilation error while importing interface as " not visible " .
Note: In a same program we cannot have both public interface and public class as because we are writing java file name as the public class name. So having two public in a single file compiler will instruct to have other public as the java file name.
Available link for download
Monday, June 26, 2017
Protected access modifier in Java
Protected access modifier in Java
Like public and private, protected is also a access modifier which has different visibility scope and it is also one of the keyword in java. Its scope are valid to the other packages but it should be within the sub-class. Like private we can be able to use protected scope with the constructor, methods, variables and inner classes. Accessing protected member outside its scope will give compilation error saying " Not Visible ".
Note : We cannot use protected with the class.
What are the possible areas where protected members can be used ?
It can be used with constructor, methods, variables and inner classes.
Members of the class declared as protected can be accessible outside the package but with the current sub-class object in the other packages. We have two scopes in java:
1. Class Scope
- Sub-class in the same package --> Visible
- Sub-class in other packages --> Visible
- Another class in the same package --> Visible
- Non- subclass in other package --> Not visible
2. Package Scope
- Within the same package ---> Visible
- Outside the package ---> Visible but in the current sub-class.
Protected with Constructor
Declaring protected keyword before the name of constructor will make it protected. Protected constructor of a class are visible in another class sub-class which is defined in another package and can only be accessible with that sub-class object. For ex-
D.java in com package
------------------------------
package com;
public class D {
protected D(){} // Protected constructor
public static void main(String[] args) {
}
}
E.java in def package
-----------------------------
package def;
import com.D;
public class E extends D {
public static void main(String[] args) {
E e = new E();
D d = new D(); // " Visible "
}
}
Protected with variable
Like constructor variable declared as protected are also visible with the sub-class object in another package. For ex-
------------------------------
package com;
public class D {
protected int x=10; // Protected variable
public static void main(String[] args) {
}
}
E.java in def package
-----------------------------
package def;
import com.D;
public class E extends D {
public static void main(String[] args) {
E e = new E();
System.out.println(e.x);
D d = new D();
System.out.pritln(d.x); // "Visible"
}
}
Protected with the method
Like variable method declared as protected can be accessed with the sub-class object defined in another package. For ex-
-----------------------------
package com;
public class D {
protected void display(){
System.out.println("protected method");
} // Protected method
public static void main(String[] args) {
}
}
E.java in def package
----------------------------
package def;
import com.D;
public class E extends D {
public static void main(String[] args) {
E e = new E(); // Current class object
e.display();
}
}
Available link for download
Friday, May 19, 2017
Private access modifier in Java
Private access modifier in Java
Private is the keyword in java and it is also called as the access modifier in java which defines the visibility to the members of the class like methods, constructors, variables etc. Declaring private to the members of the class will limit theirs visibility to that class only. If we are trying to access any private member in another class then compiler will give compilation error as " Member are not visible ".
Note : We cannot use private with the class as it makes no sense making a class visible because we are accessing member of the class not the class itself. So we can be able to use private with the member of the class member.
Where we can be able to use private access modifier ?
Private access modifier can be used with the constructor, variable and methods.
Members of the class declared as private can be accessible within that class only. We have two scope in java program :-
1. Class Scope- Sub-class in the same package --> Private is Not Visible .
- Sub-class in different package --> Private is Not Visible.
- Another class in the same package --> Private is Not Visible.
- Same class in the same package --> Private is Visible.
2. Package Scope
- Within the same package -- > Private is Not Visible.
- Outside the package ---> Private is Not Visible.
Private with constructor
To make a constructor as private we have to use private keyword before the constructor. Declaring super-class constructor as private will restrict to create an object of super-class in sub-class as private members are not accessible outside its scope. For ex-
public class prog{
private prog(){}
}
class A extends prog {
public static void main(String arg[]){
prog p= new prog(); //Cannot create object as constructor
is declared as private.
}
}
Private with variable
Like private constructor , variable can be declared as private and cannot be accessible in sub-class or any other class expect in the class in which it is declared as private. For ex-
public class prog{
private int x=10;
}
class A extends prog {
public static void main(String arg[]){
prog p= new prog();
System.out.println(x); //not visible as x is declare
as private.
}
}
Private with methods
Using private keyword before the name of the constructor makes it a private method and has accessibility limit to that class only and hence accessioning outside the class will give compilation error. For ex-
public class prog{
private void display(){}
}
class A extends prog {
public static void main(String arg[]){
prog p= new prog();
p.display(); //Compilation error as display()is private.
}
}
Available link for download
Subscribe to:
Posts (Atom)