Creating a class

One of the most repetitive tasks in software development is creating classes. Once again, with NetBeans wizards, creation of classes is very easy and straightforward.

Getting ready

It is necessary to have a project in order to create a class, so if you are unsure on how to do this, please check the Creating a Java project using Wizard recipe. However, when naming the project, enter CreatingClasses.

How to do it...

When the Projects tab shows the CreatingClasses project, expand the CreatingClasses node, if not yet expanded.

Even though NetBeans will create a main class, we will create another one:

  1. Right-click on the CreatingClasses project, and select New and Java Class....
  2. On the New Java Class window, type MyClass under Class Name.
  3. On the package selection, click on the dropdown and select creatingclasses.
  4. Add .my as a suffix in the Package. This will be shown as creatingclasses.my.
  5. Click Finish.
How to do it...

How it works...

The class is created relative to the path of the right-click. Having said that, this example shows that it is also possible to create packages during class creation.

By appending a dot (.) as a suffix, the IDE will create a Java class and a package. In this case, package creatingclasses.my and a class called MyClass.

The class created by the IDE is a very basic one. Added by the IDE are comments and package path, along with the class declaration.

There's more...

Discovering hidden file types with the Other option.

Other file types

Any number of files can be created by using this method. If curiosity strikes, check the files under Other... after right-clicking on a project name and selecting New.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.10.69