proper use of 'this' in Java -
i'm beginner java programmer , wanted on code i'm writing please can confirm if right use of this
keyword
public class readfile { private string path; // string store filename , path. public readfile(string path){ this.path; } }
within instance method or constructor, this
reference current object — object method or constructor being called. can refer member of current object within instance method or constructor using this
as answers above suggest, can this
pointing member variable , use invoke constructor within constructor.
see below example
public class rectangle { private int x, y; private int width, height; public rectangle() { this(0, 0, 1, 1); } public rectangle(int width, int height) { this(0, 0, width, height); } public rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } ... }
refer here more information.
hope helps
Comments
Post a Comment