Thu Jul 28, 2011 5:47 pm
class ExampleClass {
int x;
int y;
int sum;
ExampleClass() {
sum=x+y;
}
}
public class ExampleClass {
public int x;
public int y;
public int sum;
public ExampleClass() {
sum=x+y;
}
}
class ExampleClass {
protected int x;
protected int y;
protected int sum;
protected ExampleClass() {
sum=x+y;
}
void setY()
{
y=3;
}
}
class ExampleInherit extends ExampleClass
{
void setX()
{
x=4;
setY();
}
void setY()
{
y=544;
}
void setY(int decimal)
{
y=decimal;
}
}
class ExampleClass {
protected int x;
protected int y;
protected int sum;
protected ExampleClass() {
sum=x+y;
}
protected class ExampleInnerClass{
}
void setY()
{
y=3;
}
}
class ExampleClass {
private int x;
private int y;
private int sum;
protected ExampleClass() {
sum=x+y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
class ExampleInherit extends ExampleClass
{
void setX()
{
setX(3);
setY(4);
}
}
class ExampleClass {
private int x;
private int y;
private int sum;
protected ExampleClass() {
sum=x+y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
private class ExampleInnerClass{
}
}
public class ExampleClass {
private ExampleClass() {
}
public static void main(String[] args) {
ExampleClass ec=new ExampleClass();
}
}
public class Main {
public static void main(String[] args) {
ExampleClass2 exampleClass2= new ExampleClass2(); // Private Constructor Error
}
}
class ExampleClass2 {
private ExampleClass2() {
}
}
Private constructors are usually used design patterns such as singleton:
[code2=java]
public class Main {
public static void main(String[] args) {
ExampleClass2 exampleClass2= ExampleClass2.getInstance();
}
}
class ExampleClass2 {
private static ExampleClass2 exampleClass2;
private ExampleClass2() {
}
public static ExampleClass2 getInstance()
{
if(exampleClass2==null)
{
exampleClass2= new ExampleClass2();
}
return exampleClass2;
}
}
Mon Jun 10, 2013 3:18 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.