Wednesday, July 22, 2020

"this" keyword in java

"this" keyword


If instance variable and local variable name is same i.e. a=a in example, it'll print the value of instance value:



package code;

public  class ThisKeywordTest {
 int a;


 void setValue(int a) {
a=a;

}
 void show() {
System.out.println(a);
}


public static void main(String[] args) {

ThisKeywordTest ob = new ThisKeywordTest();
ob.setValue(4);  
ob.show();
}

}
o/p: 0
---------------------------------------------------------------------------------------------

To resolve the issue we'll use this keyword.

package code;

public  class ThisKeywordTest {
 int a;


 void setValue(int a) {
 this.a=a;

}
 void show() {
System.out.println(a);
}


public static void main(String[] args) {

ThisKeywordTest ob = new ThisKeywordTest();
ob.setValue(4);
ob.show();
}


}
o:p= 4




Add caption





CONSTRUCTOR in java

Why we need constructor in java?

Before constructor, we initialize the variables and object by creating objects. e.g.
ob.emp_id=101;
ob.emp_name="Manoj";
.
.
.

If there are 1000 employes in the company we need to created 1000 objects and 2000 lines to intialize the values of object.{which is not good programming}
but after using constructor, we can intialize the values of instance variable by using parameterizes constructor.

Employee ob = new Employee(102,"Parisha");

There are 3 types of constructor:
1.)Default constructor,
2.)User defined constructor: no-argument constructor, parameterized constructor.
NOTE:
1.) constructor is created by COMPILER, Not by JVM.
2.) If user defined constructor is created then , Default constructor will not be Created. compiler tabhi default constructor create krega jb hum khud se koi constructor create nhi krengey.

constructor ka return type kyo nahihota?
answer:
1.) constructor ka kam object ko initialize krna hota hai, jb onject k ander values initialize krni hai to return type to wo kuch krega hi nahi.

2.) Compiler ye predict nahi kr skta k constructor ka return type kya hoga.








Thursday, July 9, 2020

Category and product synchronization


ProductOperationServlet.java


protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub

PrintWriter out = response.getWriter();

String op = request.getParameter("operation");

if (op.trim().equals("addcategory")) {
// fetching data from category form
String title = request.getParameter("catTitle");
String description = request.getParameter("catDesc");

// create category objet to set an attribute
Category ct = new Category();
ct.setCategoryTitle(title);
ct.setCategoryDescription(description);

// category save in database
CategoryDao categoryDao = new CategoryDao(FactoryProvider.getFactory());
int catId = categoryDao.saveCategory(ct);

HttpSession session = request.getSession();
session.setAttribute("message", "New category added successfully!!"+catId);
response.sendRedirect("admin.jsp");
return;
} else if (op.trim().equals("addProduct")) {
System.out.println("running addproduct conditionnnnnnnnnnn");
String pName = request.getParameter( "pName");
String pPhoto = request.getParameter("pPhoto");
String pDesc = request.getParameter("pDesc");
int pPrice = Integer.parseInt(request.getParameter(("pPrice")));

int pDiscount = Integer.parseInt(request.getParameter("pDiscount"));

int pQuantity = Integer.parseInt(request.getParameter("pQuantity"));
int catId = Integer.parseInt(request.getParameter("catId"));
Part part = request.getPart("pPhoto");

Product p = new Product();
p.setpName(pName);
p.setpPhoto(pPhoto);
p.setpDesc(pDesc);
p.setpPrice(pPrice);
p.setpDiscount(pDiscount);
p.setpQuantity(pQuantity);
p.setpPhoto(part.getSubmittedFileName());

// get Category by id
CategoryDao cdao = new CategoryDao(FactoryProvider.getFactory());
Category category = cdao.getCategoryById(catId);
p.setCategory(category);



//product save 
//constructor ask for SessionFactory
ProductDao pdao = new ProductDao(FactoryProvider.getFactory());
//pic upload
       //Find out the path to upload photo
@SuppressWarnings("deprecation")
String path = request.getRealPath("img") + File.separator + "products"+ File.separator +
part.getSubmittedFileName();

out.println("path:: "+path);

//uploading data
try {

FileOutputStream fos = new FileOutputStream(path);
InputStream is = part.getInputStream();
//reading data
byte[] data=new byte[is.available()];
is.read(data);

//writing the data
fos.write(data);
fos.close();


} catch (Exception e) {
// TODO: handle exception
}

Tuesday, July 7, 2020

Singleton example


CODINGROUTE BLOG represents some code for daily usage.


How to create Singleton design pattern?

To create the singleton class, we need to have static member of class, private constructor and static factory method.

  • Static member: It gets memory only once because of static, itcontains the instance of the Singleton class.
  • Private constructor: It will prevent to instantiate the Singleton class from outside the class.
  • Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller.

Guitar Chords

click on   Guitar Chords Family