'Back-End/Java_1'에 해당되는 글 48건

[Java] Constructor

Back-End/Java_1 2016. 5. 17. 14:00
donaricano-btn

Constructor

- Constructors are required to create objects for a class. Constructors are used to initialize the instance variable of an object

- It must have the same name as that of the class and have no return type

- If you don't define a constructor, then the compiler creates a default constructor

- Use this() to communicate from one constructor to another constructor in the same class

- Use super() to communicate with super class constructor


1. Default Constructor

- You can also call a constructor without parameters as default constructor because all of its class instance variable are set to default values


2. Constructor Overloading 

- Like method overloading we can overload constructors also


3. Constructor chaining

- Calling another constructor in the same class from another constructor is called constructor chaining.

By using this() we can call another constructor in the same class

this() should be the first line the constructor


4. SingleTon 

- We can make constructor as private. so that we can not create an object outside of the class

Singleton Pattern helps us to keep only one instance of a class at any time





'Back-End > Java_1' 카테고리의 다른 글

[Java] JDK, JRE, and JVM  (0) 2016.06.03
[Java] Exception  (0) 2016.05.18
[Java] Enum  (0) 2016.05.13
[Java] Array  (0) 2016.05.12
[Java] Static  (0) 2016.05.11
블로그 이미지

리딩리드

,

[Java] Enum

Back-End/Java_1 2016. 5. 13. 11:27
donaricano-btn

Enum

- Enum constants are by default public static final fields. If you declare Enum is a member of your class, they by default it is static, We should not use new operator with enum type at anytime


1. Example


2. How to call Enums in class

If you declare Enum is a member of a class, then by default is is static. you can access it with reference to enclosing class


3. How to override toString() with enum

By default the enum toString() method returns the constant name itself. you can change return value by overriding toString() method


4. How to create custom constructor enum

The constructor should be either private or default scope, should not be protected or public. All elements defined in the enum must call constructor


'Back-End > Java_1' 카테고리의 다른 글

[Java] JDK, JRE, and JVM  (0) 2016.06.03
[Java] Exception  (0) 2016.05.18
[Java] Constructor  (0) 2016.05.17
[Java] Array  (0) 2016.05.12
[Java] Static  (0) 2016.05.11
블로그 이미지

리딩리드

,

[Java] Array

Back-End/Java_1 2016. 5. 12. 13:30
donaricano-btn

Array

Array is a group of same kind of variables and can be accessible by a common name


1. How to convert Array to List in java

Utility method Arrays.asList() helps us to convert an Array of objects to List of objects



2. BinarySearch

Arrays.binarySearch() helps us to find an object from an array of objects by using binary search algo


3. Copy array and increase size dynamically

Arrays.copyOf() helps us to create new array with new size and copy old arrays content to the new array at the same time


4. Copy range of elements from an array

Arrays.copyOfRange() helps us to copy range of object from existing array to new array


5. Compare two arrays and confirm they are equal

Arrays.deepEquals() helps us to compare two arrays

Arrays.deepEquals() can compare only object(not int)




6. Fill an Array with default 

Arrays.fill() helps us to fill an empty array with default values


7. Sort an array

Arrays.sort() helps us to sort an array of object


8. Sort an array using comparator

Arrays.sort() helps us to sort an array of objects by passing Comparator object,where Comparator holds the sorting logic



'Back-End > Java_1' 카테고리의 다른 글

[Java] JDK, JRE, and JVM  (0) 2016.06.03
[Java] Exception  (0) 2016.05.18
[Java] Constructor  (0) 2016.05.17
[Java] Enum  (0) 2016.05.13
[Java] Static  (0) 2016.05.11
블로그 이미지

리딩리드

,

[Java] Static

Back-End/Java_1 2016. 5. 11. 15:16
donaricano-btn

Static

a static member is a member of a class that isn't associated with an instance of a class. Instead, the member belongs to the class itself. As a result, you can access the static member without first creating a class instance


1. Example for static variable and methods

1_1. static variable

Static variables are belongs to the class and not to the object


1_2. static method

Static methods are also similar to static variable


1_3. Example


2. Example for static block

Static blocks are nothing but a normal block of code, enclosed in braces {}, preceded with static keyword.

These static blocks will be called when JVM loads the class into memory. Incase a class has multiple static blocks across the class, then JVM combines all these blocks as a single block of code and executes it


2_1. Example


3. Example for static block vs constructor

Java static blocks will be called when JVM loads the class into memory, means it will be called only once. But constructor will be called everytime when you create an object. 


3_1 Example


4. Example for Singleton class using static block

Since static block will be called only once, we can use static block to develop singleton class.

To create singleton class, make constructor as private, so that you cannot create object outside of the class. Create a private static variable of same class type, so that created object will be pointed to this reference. Now create static block, and create object inside static block. Since static block will be called only once, the object will be created only once.


4_1. Example


5. STATIC import

 We can access any static fields or methods with reference to the class name. Static imports allow us to import all static fields and methods into a class and you can access them without class name reference

5_1. Source






'Back-End > Java_1' 카테고리의 다른 글

[Java] JDK, JRE, and JVM  (0) 2016.06.03
[Java] Exception  (0) 2016.05.18
[Java] Constructor  (0) 2016.05.17
[Java] Enum  (0) 2016.05.13
[Java] Array  (0) 2016.05.12
블로그 이미지

리딩리드

,