Gordon  Murray

Gordon Murray

1669613100

How to Generate Getters and Setters To A Java Or Kotlin Class

Android Studio - How to generate getters and setters to a class

Android Studio provides you with a helper menu to generate getters and setters for class fields.

Here’s how to generate getters and setters for Java class fields:

  • Press command + N for Mac
  • Press Alt + Insert for Windows and Linux.

In the following screenshot, you can see the Generate setter and getter option highlighted from the Generate menu:

Android Studio generate getter and setter optionAndroid Studio generate getter and setter option

You can also reach the same menu by right-clicking anywhere inside your class in the code editor and select the Generate… option.

A context menu should appear as shown below:

Android Studio Generate context menuAndroid Studio Generate context menu

Alternatively, you can also use the top bar Code menu to generate getters and setters for a class:

Code > Generate… > Getter and Setter

Inside the Getter and Setter menu, select all the fields you want to generate getters and setters methods and click OK.

You should see getters and setters generated in your code as shown below:

public class User {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

And that’s how you generate getters and setters for a Java class.

Generate getters and setters for a Kotlin class

By default, Kotlin automatically generate getters and setters for all your class properties.

When you define the following code in Kotlin:

class Profile {
    var name: String = "Nathan"
    var age: Int = 29
}

Getters and setters will be auto-generated in your class like this:

class Person {
    var name: String = "Nathan"
    var age: Int = 29

    // getter
    get() = field

    // setter
    set(value) {
        field = value
    }
}

In Kotlin, there’s no need to define getters and setters manually to get the class attribute or property values.

The following Java code:

private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

Is equivalent to the following Kotlin code:

var name: String = ""

In Kotlin, class fields (or properties) are always accessed through the getter and setter of that class. When you run the classname.property = value assignment, the set() function is called internally.

When you try to get property value using the classname.property syntax, then the get() function is called.

There’s no need to create private properties with public accessors/ mutators like in Java.

For this reason, Android Studio doesn’t provide an option to generate getters and setters for Kotlin classes.

At times, you might want to set the setters as private to prevent change outside of the class. You can do so with the following code:

class Profile {
    var name: String = "Nathan"
    private set
    var age: Int = 29
    private set
}

You need to set the access level of the setters individually for each property.

Now you’ve learned how to generate getters and setters method with the help of Android Studio. Good work! 👍

Original article source at: https://sebhastian.com/

#androidstudio #java #kotlin #class 

What is GEEK

Buddha Community

How to Generate Getters and Setters To A Java Or Kotlin Class
Tyrique  Littel

Tyrique Littel

1600135200

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK?

OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE). It contains the virtual machine, the Java Class Library, and the Java compiler. The difference between the Oracle OpenJDK and Oracle JDK is that OpenJDK is a source code reference point for the open-source model. Simultaneously, the Oracle JDK is a continuation or advanced model of the OpenJDK, which is not open source and requires a license to use.

In this article, we will be installing OpenJDK on Centos 8.

#tutorials #alternatives #centos #centos 8 #configuration #dnf #frameworks #java #java development kit #java ee #java environment variables #java framework #java jdk #java jre #java platform #java sdk #java se #jdk #jre #open java development kit #open source #openjdk #openjdk 11 #openjdk 8 #openjdk runtime environment

Lawrence  Lesch

Lawrence Lesch

1662107520

Superdom: Better and Simpler ES6 DOM Manipulation

Superdom

You have dom. It has all the DOM virtually within it. Use that power:

// Fetch all the page links
let links = dom.a.href;

// Links open in a new tab
dom.a.target = '_blank';

Only for modern browsers

Getting started

Simply use the CDN via unpkg.com:

<script src="https://unpkg.com/superdom@1"></script>

Or use npm or bower:

npm|bower install superdom --save

Select

It always returns an array with the matched elements. Get all the elements that match the selector:

// Simple element selector into an array
let allLinks = dom.a;

// Loop straight on the selection
dom.a.forEach(link => { ... });

// Combined selector
let importantLinks = dom['a.important'];

There are also some predetermined elements, such as id, class and attr:

// Select HTML Elements by id:
let main = dom.id.main;

// by class:
let buttons = dom.class.button;

// or by attribute:
let targeted = dom.attr.target;
let targeted = dom.attr['target="_blank"'];

Generate

Use it as a function or a tagged template literal to generate DOM fragments:

// Not a typo; tagged template literals
let link = dom`<a href="https://google.com/">Google</a>`;

// It is the same as
let link = dom('<a href="https://google.com/">Google</a>');

Delete elements

Delete a piece of the DOM

// Delete all of the elements with the class .google
delete dom.class.google;   // Is this an ad-block rule?

Attributes

You can easily manipulate attributes right from the dom node. There are some aliases that share the syntax of the attributes such as html and text (aliases for innerHTML and textContent). There are others that travel through the dom such as parent (alias for parentNode) and children. Finally, class behaves differently as explained below.

Get attributes

The fetching will always return an array with the element for each of the matched nodes (or undefined if not there):

// Retrieve all the urls from the page
let urls = dom.a.href;     // #attr-list
  // ['https://google.com', 'https://facebook.com/', ...]

// Get an array of the h2 contents (alias of innerHTML)
let h2s = dom.h2.html;     // #attr-alias
  // ['Level 2 header', 'Another level 2 header', ...]

// Get whether any of the attributes has the value "_blank"
let hasBlank = dom.class.cta.target._blank;    // #attr-value
  // true/false

You also use these:

  • html (alias of innerHTML): retrieve a list of the htmls
  • text (alias of textContent): retrieve a list of the htmls
  • parent (alias of parentNode): travel up one level
  • children: travel down one level

Set attributes

// Set target="_blank" to all links
dom.a.target = '_blank';     // #attr-set
dom.class.tableofcontents.html = `
  <ul class="tableofcontents">
    ${dom.h2.map(h2 => `
      <li>
        <a href="#${h2.id}">
          ${h2.innerHTML}
        </a>
      </li>
    `).join('')}
  </ul>
`;

Remove an attribute

To delete an attribute use the delete keyword:

// Remove all urls from the page
delete dom.a.href;

// Remove all ids
delete dom.a.id;

Classes

It provides an easy way to manipulate the classes.

Get classes

To retrieve whether a particular class is present or not:

// Get an array with true/false for a single class
let isTest = dom.a.class.test;     // #class-one

For a general method to retrieve all classes you can do:

// Get a list of the classes of each matched element
let arrays = dom.a.class;     // #class-arrays
  // [['important'], ['button', 'cta'], ...]

// If you want a plain list with all of the classes:
let flatten = dom.a.class._flat;     // #class-flat
  // ['important', 'button', 'cta', ...]

// And if you just want an string with space-separated classes:
let text = dom.a.class._text;     // #class-text
  // 'important button cta ...'

Add a class

// Add the class 'test' (different ways)
dom.a.class.test = true;    // #class-make-true
dom.a.class = 'test';       // #class-push

Remove a class

// Remove the class 'test'
dom.a.class.test = false;    // #class-make-false

Manipulate

Did we say it returns a simple array?

dom.a.forEach(link => link.innerHTML = 'I am a link');

But what an interesting array it is; indeed we are also proxy'ing it so you can manipulate its sub-elements straight from the selector:

// Replace all of the link's html with 'I am a link'
dom.a.html = 'I am a link';

Of course we might want to manipulate them dynamically depending on the current value. Just pass it a function:

// Append ' ^_^' to all of the links in the page
dom.a.html = html => html + ' ^_^';

// Same as this:
dom.a.forEach(link => link.innerHTML = link.innerHTML + ' ^_^');

Note: this won't work dom.a.html += ' ^_^'; for more than 1 match (for reasons)

Or get into genetics to manipulate the attributes:

dom.a.attr.target = '_blank';

// Only to external sites:
let isOwnPage = el => /^https?\:\/\/mypage\.com/.test(el.getAttribute('href'));
dom.a.attr.target = (prev, i, element) => isOwnPage(element) ? '' : '_blank';

Events

You can also handle and trigger events:

// Handle click events for all <a>
dom.a.on.click = e => ...;

// Trigger click event for all <a>
dom.a.trigger.click;

Testing

We are using Jest as a Grunt task for testing. Install Jest and run in the terminal:

grunt watch

Download Details:

Author: franciscop
Source Code: https://github.com/franciscop/superdom 
License: MIT license

#javascript #es6 #dom 

Gordon  Murray

Gordon Murray

1669613100

How to Generate Getters and Setters To A Java Or Kotlin Class

Android Studio - How to generate getters and setters to a class

Android Studio provides you with a helper menu to generate getters and setters for class fields.

Here’s how to generate getters and setters for Java class fields:

  • Press command + N for Mac
  • Press Alt + Insert for Windows and Linux.

In the following screenshot, you can see the Generate setter and getter option highlighted from the Generate menu:

Android Studio generate getter and setter optionAndroid Studio generate getter and setter option

You can also reach the same menu by right-clicking anywhere inside your class in the code editor and select the Generate… option.

A context menu should appear as shown below:

Android Studio Generate context menuAndroid Studio Generate context menu

Alternatively, you can also use the top bar Code menu to generate getters and setters for a class:

Code > Generate… > Getter and Setter

Inside the Getter and Setter menu, select all the fields you want to generate getters and setters methods and click OK.

You should see getters and setters generated in your code as shown below:

public class User {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

And that’s how you generate getters and setters for a Java class.

Generate getters and setters for a Kotlin class

By default, Kotlin automatically generate getters and setters for all your class properties.

When you define the following code in Kotlin:

class Profile {
    var name: String = "Nathan"
    var age: Int = 29
}

Getters and setters will be auto-generated in your class like this:

class Person {
    var name: String = "Nathan"
    var age: Int = 29

    // getter
    get() = field

    // setter
    set(value) {
        field = value
    }
}

In Kotlin, there’s no need to define getters and setters manually to get the class attribute or property values.

The following Java code:

private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

Is equivalent to the following Kotlin code:

var name: String = ""

In Kotlin, class fields (or properties) are always accessed through the getter and setter of that class. When you run the classname.property = value assignment, the set() function is called internally.

When you try to get property value using the classname.property syntax, then the get() function is called.

There’s no need to create private properties with public accessors/ mutators like in Java.

For this reason, Android Studio doesn’t provide an option to generate getters and setters for Kotlin classes.

At times, you might want to set the setters as private to prevent change outside of the class. You can do so with the following code:

class Profile {
    var name: String = "Nathan"
    private set
    var age: Int = 29
    private set
}

You need to set the access level of the setters individually for each property.

Now you’ve learned how to generate getters and setters method with the help of Android Studio. Good work! 👍

Original article source at: https://sebhastian.com/

#androidstudio #java #kotlin #class 

Samanta  Moore

Samanta Moore

1620458875

Going Beyond Java 8: Local Variable Type Inference (var) - DZone Java

According to some surveys, such as JetBrains’s great survey, Java 8 is currently the most used version of Java, despite being a 2014 release.

What you are reading is one in a series of articles titled ‘Going beyond Java 8,’ inspired by the contents of my book, Java for Aliens. These articles will guide you step-by-step through the most important features introduced to the language, starting from version 9. The aim is to make you aware of how important it is to move forward from Java 8, explaining the enormous advantages that the latest versions of the language offer.

In this article, we will talk about the most important new feature introduced with Java 10. Officially called local variable type inference, this feature is better known as the **introduction of the word **var. Despite the complicated name, it is actually quite a simple feature to use. However, some observations need to be made before we can see the impact that the introduction of the word var has on other pre-existing characteristics.

#java #java 11 #java 10 #java 12 #var #java 14 #java 13 #java 15 #verbosity

Joseph  Murray

Joseph Murray

1624063200

Creating an Immutable Class in Java

In this article, we’ll define the typical steps for creating an immutable class in Java and then implement it.

Steps to create Immutable class in java

  1. Make your class final, so that no other classes can extend it.
  2. Make all fields private so that direct access is not allowed.
  3. Don’t provide “setter” methods — methods that modify fields or objects referred to by fields.
  4. Special attention when having mutable instance variables
  5. Inside the constructor, make sure to use a clone copy of the passed argument and never set your mutable field to the real instance passed through the constructor.
  6. Make sure to always return a clone copy of the field and never return the real object instance.

#java #class #objects #immutable #creating an immutable class in java #immutable class in java