Category Archives: Programming Languages

List of Java Tutorial Examples and Source Codes for Absolute Beginners

List of Java Tutorial Examples and Source Codes for Absolute Beginners
Hacking Exposed : Network Security Secrets and Solutions No. 6 by Stuart…
Current Bid: $18.00

A Collection of Java Tutorial Examples and Running Source Code

Are you a fan of Java Programming Language? If so, here is the collection of my Java tutorial for beginners that contain many example Source codes. What do you want to know about Java? Do you want to learn more about Java? The links below is the list of example tutorials that you might find useful in your learning on this programming language.

Lists of Java Tutorial Examples for beginners

Java Tips and Tutorials

A Beginners Guide on Learning Java
Java Simple Codes for Beginners
Steps On How to Program in Java Using Netbeans
Basic Knowledge Require in Programming
5 Important Tips on Learning Programming
Learn More About Java Classes
How To Run Your Java Code Online
Guide to Install Netbeans IDE in your PC
Guide to Install Eclipse IDE for Java Programming
How to Make Programs on Eclipse IDE? Eclipse IDE Guide
What is the Best IDE to Use for Java Programming?

Speed Up Your PC!

System Mechanic PC TotalCare – Fix, Speed Up & Protect (3PCs) New
Current Bid: $10.50

Java Source Codes Examples

How to use ‘if statement’ in Java Programming
How to use ‘if and else statement’ in Java Programming
Java Program for Multi if else Branching Mechanism
Java Program for Switch Statements
Java Program: How to Parse String into Integer in Java Programming
Java Program: Count the Number of String Characters in Java
Java Program: Reverse String in Java Using For Loop
Java Program: Palindrome Test Java Source Code
Sample Java Program for String Tokenizer
Sample Java Program for JOptionPane with Basic Calculator
What is a Java Scanner Class?
Sample Java Source Code for MDAS
Casting in Java
Static Method in Java
BufferedReader in Java
Sort Numbers in Ascending Order
Sort Numbers in Descending Order
Count Number of Vowels in String Java Program
Adding Numbers Inside Array Using For Loop
Adding Numbers Inside Array Using Recursion
Print Different Asterisk Form Using For Loop
Recursive Diamond Shape Asterisk
Binary Search in Recursion
Linear Search in Recursion
Sort Numbers in Bubble Sort
Sort Numbers in Selection Sort
Java Source Code on Power
Java Source Code on Solving Age and Salutation
Printing A String Backward Using Recursion
Koch Snow Flakes in Java

Java Books for Beginners

NEW Java 7 For Absolute Beginners – Bryant, Jay
Current Bid: $27.26
Android Boot Camp for Developers Using Java : A Beginner’s Guide to Creating…
Current Bid: $2.29

Learning Java Programming is not hard if you focus your mind to it. The links above shows on how to program in java. By providing those tips and sample java source codes, beginners might understand on how the java programming works. Test and run it on your own java editors, trace the codes and figure out why it outputs like that. To Information Technology, Computer Science Students and to other programming enthusiasts enjoy your java programming!

Java Video Tutorials

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99

Interesting Reads!

What are the Most Popular Programming Languages in the World?
Most In Demand Skills and Jobs for IT and Computer Science Graduates
What is the Best Scripting Language to Learn
What are the Best Programming Languages to Learn?

Java Program: How to Parse a String into Integer in Java Programming

Java Program: How to Parse a String into Integer in Java Programming

Java Program: Parse String into Integer in Java Programming with a Sample Java Source Code

Parsing in Java is also a known practice by the Java Programmers. There are many reasons why a programmer parse a string into integer and for the sake of knowledge let us know how parsing takes place. Parsing usually use when extracting integers inside a string entered by the user. I first used it in a basic calculator program using joptionpane and jframe. The series of numbers and operators is entered by the user and the challenge is to solve it, identify the numbers and the operators inside the string and output the correct answer. However, in the following Java Source Code, we will just cover on how to use the parseInt() in Java. The program will compute the string entered by the user. So, to compute it, we will parse the string into integer. See the Java Source Code below.

Related Hub for String

Sample Java Program for String Tokenizer
Java Program: Palindrome Test Java Source Code
Java Program: Count the Number of String Characters in Java
Java Source code: Reverse String in Java Using Recursion

Java Source Code on How to Parse a String into Integer

package parsing_in_java;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(“Enter a number: “);
String string_num1 = input.nextLine(); // string is use instead of int
System.out.print(“Enter another number: “);
String string_num2 = input.nextLine();
int integer_num1 = Integer.parseInt(string_num1);//this is how to parse string
int integer_num2 = Integer.parseInt(string_num2);//another example on parsing
System.out.println(“The sum is: ” + (integer_num1 + integer_num2));
}
}

Sample Output:

Do you find this hub useful to you? Then, please rate it or leave a comment. Happy Programming!

Related Java Tutorials

5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples

Other Java Source Code Samples

Java Program: How to Use If Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
Java Program: How to Use If Statement in Java Programming
Java Program: How to Use Switch Statement in Java
Class in Java: Learn More about Java Classes with Sample Java Codes
Java Source Code: Sort Numbers in Selection Sort
Java Source Code Recursion: Recursive Koch Snow Flakes
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source Code in a Recursive Linear Search
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Code Sample: Sort Numbers in Bubble Sort
Java Source Code Determine Person’s Salutation and Current Age
Java source code in Recursive function for X to the power Y
Java Source Code: Binary Search in Recursion
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java source code Sample: Print Different Asterisk Shapes in Recursion

Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion

Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96

Below is the Java sample code on printing the GCD of the 2 numbers in recursion. The GCD recursive methods is quite an easy problem to program if you know its recursive definition. Since recursion is basically has a base case and general case, the program will stop on looping if it meets the condition of the base case and return a value of the general case if not. To have a glimpse of the recursive formula, here it is for you to evaluate and see how I transform it into java language.

GCD Recursive Definition:

Given two integers x and y

{ x if y==0
GCD(x, y) =
{ y if y != 0

Below is the following code for the recursive definition above.

Java Source Code on Printing the Greatest Common Divisor or GCD in Recursion

//java class
public class GCD
{
public static int gcd(int num1, int num2)
{
if(num2 == 0)
{
return num1;
}
else
{
return gcd(num2, num1%num2);
}
}
}
//main class
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(“Enter the base number: “);
int num1 = input.nextInt();
System.out.print(“Enter the power: “);
int num2 = input.nextInt();
GCD access = new GCD();
System.out.print(“The GCD of 2 numbers is: ” + access.gcd(num1, num2));
}
}

Java Tutorials and Tips

Complete List of Java Tutorial and Source Codes for Absolute Beginners
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Class: Learn More About Classes in Java

Here is the sample output of the program

Sample Output:

Enter the base number: 100

Enter the power: 45

The GCD of 2 numbers is: 5

Sample Output 2:

Enter the base number: 6

Enter the power: 8

The GCD of 2 numbers is: 2

Java Video Tutorials

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99

Java Books

Core Java 2 Advanced Features Vol. II by Gary Cornell and Cay Horstmann…
Current Bid: $3.99
No Photo
(2 December, 2004) Core Java(TM) 2, Volume II–Advanced Features (7th Edition)
Current Bid: $10.00

Other Java Source Code Examples

Java Simple Codes for Beginners
What is a Java Scanner Class and How to Use it?
Java Program: How to Use Switch Statement in Java
Java source codes on outputting Asterisk in Different Forms Using Recursion
Java Source Code: A Recursive Asterisk Diamond Shape
Java Source code on Adding Numbers inside the Array using For Loop
Java Source codes on Adding numbers inside the Array Using Recursion
Java Source Code: How to Sort Numbers in Bubble Sort Using Recursion
Java Source Code: Sort Numbers using Selection Sort
Java Source Code on Linear Search Using Recursion
Java Source Code: Binary Search in Recursion
Java Source code: How to Print a String Backward using Recursion
Java source code: How to Output the Answer of the Integer X to the Power Y
Java Source Code: How to make a Program that will determine a Person’s Salutation and Current Age
Java Source Code: Recursive Snow Flakes

Java Source Code: A Recursive Asterisk Diamond Shape Program

Java Source Code: A Recursive Asterisk Diamond Shape Program
Excellent Condition Iphone 4 8gb LIKE NEW! Cheap Price!!
Current Bid: $46.00

Below is the Java source code on getting a diamond shape of asterisks using recursion. Recursion is way that repeat the codes over and over until the condition is met. It is like the for, while and do-while loops. This program I made on outputting a diamond shape asterisk prompts for the user to enter the number and the program will output the asterisks in diamond shape. The size of the diamond depends on the number that the user will enter. You can study and trace the code below.

Java Source Code: A Recursive Asterisk Diamond Shape

//java class
public class Diamond
{
public String Diamond_Asterisk(int num) //method1
{
if(num>0)
{
return “*-” + Diamond_Asterisk(num-1);
}
else
{
return “-“;
}
}
public String Diamond_Asterisk2(int num)//method2
{
if(num>0)
{
return “-*-” + Diamond_Asterisk(num-1);//access method1
}
else
{
return “-“;
}
}
public String Space(int num) //method3
{
if(num>0)
{
return “-” + Space(num-1);
}
else
{
return “-“;
}
}
public void DiamondResult(int num)//method4
{
for(int i=1; iJava Tips and Tutorials

Java Class: Learn More About Classes in Java
Java Tutorial Examples
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Simple Codes for Beginners
How to Program in Java: Complete Simple Easy Steps
Basic Knowledge Required in Programming
5 Important Tips to Learn Java Programming and Other Programming Languages

Note: You can revise the program by changing the dashes into spaces, I just use the dash in order to show the diamond shape generated by the program.

Sample Output 1:

Enter a number: 3

The shape for this is:

—*–

–*-*–

-*-*-*–

–*-*–

—*–

Sample Output 2:

Enter a number: 4

The shape for this is:

—-*–

—*-*–

–*-*-*–

-*-*-*-*–

–*-*-*–

—*-*–

—-*–

Java Video Tutorials

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99

Sample Output 3:

Enter a number: 5

The shape for this is:

—–*–

—-*-*–

—*-*-*–

–*-*-*-*–

-*-*-*-*-*–

–*-*-*-*–

—*-*-*–

—-*-*–

—–*–

Java Books

Java Programming : From Problem Analysis to Program Design by D. S. Malik…
Current Bid: $2.99
Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: $50.80
Source: facebook.com via google image

Other Java Source Code Example

Java Simple Codes for Beginners
A Java source code: How to Output Asterisk in Different Forms Using Recursion
Java Source code: How to Add Numbers inside an Array using For Loop
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source Code: How to Sort Numbers using Selection Sort
Java Source Code: How to Sort Numbers in Bubble Sort
Java Source code: How to Print a String Backward using Recursion
Java Source Code: Binary Search in Recursion
Java Source Code on a Recursive Linear Search
Java source code: Recursive function for X to the power Y
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source Code: How to make a Program that will determine a Person’s Salutation and Current Age
Java Source Code: Recursive Snow Flakes

Do You Like Java Programming?

Yes
No
See results without voting

Java Source Code in Sorting: How to Sort Numbers in Ascending Order

Java Source Code in Sorting: How to Sort Numbers in Ascending Order
WIFI CRACK 4GB BOOTABLE USB FOR WEP WPA WPA2 hack cracking software decoding
Current Bid: $30.00

Java Program: Sort Numbers inside an array in an Ascending Order with a Sample Java Source Code

Sorting in Java is always there when you are learning java programming. Sorting numbers in ascending order shows on how ‘nested for loops’ work together. This program also uses array with a fix size. Rather than declaring a variable per entered number let just assign an array to hold a multiple of numbers. That way the program is much shorter and executes better. So, to sort numbers in Java we will have this following algorithm: If the 1st entered number in an array is greater than the 2nd, the program should swap the 2 numbers and let the smallest number comes first. Otherwise, the program would not do anything. Then the program would print the sorted numbers in the screen. See the Java Source code below.

package sort_numbers_in_ascending_order;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(“Enter 6 Numbers to Sort: “);
int num;
int[] num_in_array = new int[6];
for(int enter_num=0; enter_num<6; enter_num++) { num_in_array[enter_num] = input.nextInt(); for(int sort_num=0; sort_num<6; sort_num++) { if(num_in_array[enter_num] < num_in_array[sort_num]) { int swap = num_in_array[enter_num]; num_in_array[enter_num] = num_in_array[sort_num]; num_in_array[sort_num] = swap; } } } System.out.println("The Sorted Numbers are: "); for(int print = 0; print<6; print++) { System.out.print(num_in_array[print]+ " "); } System.out.println(); } }

Sample Output:

Related Java Tutorials:

List of Java Tutorial Examples and Source Codes for Absolute Beginners
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming

Java source code in Recursive function for X to the power Y

Java source code in Recursive function for X to the power Y
HTC EVO 4G – PC36100 (Sprint) Smartphone w/ 8GB SD Works GREAT
Current Bid: $21.50

Recursive Power Program

Below is a java source code for the recursive function of x to the power y. This program again is dealing with recursion, it just prompt the user to enter the base number and base power and the program will solve the correct answer. If the power of the base is zero it simply return 1, if the power is 1 the program will return the value of x, if it greater than one it just multiply the base number many times as the power indicated, and if the power is negative, the program simply multiply the power into -1, get the answer of the x raise to power y and finally divided it on 1. To understand the formula more, below is the recursive definition of the program.

Recursive Definition:

{1 if y=0
power(x, y) = {x if y=1
{x + power(x, y-1) if y>1
1
if y<0, power(x, y) = _____________ power(x, -y)

Java source code: Recursive function for X to the power Y

// java class
public class Power
{
public static double power(double base, double basePow)
{
if(basePow==0)
{
return 1;
}
else if(basePow==1)
{
return base;
}
else if(basePow>1)
{
return base*power(base,basePow-1);
}
else
{
return 1/power(base, -1 * basePow);
}
}
}
//main class
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(“Enter the base number: “);
double base = input.nextInt();
System.out.print(“Enter the base power: “);
double basePow = input.nextInt();
Power access = new Power();
System.out.print(base + ” to the power of ” + basePow + ” is: ” + access.power(base, basePow));
}
}

Java Tutorials and Tips

Java Tutorial Examples
5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Class: Learn More About Classes in Java

Below is the sample output of the program.

Sample Output 1:

Enter the base number: 10

Enter the base power: 0

10.0 to the power 0.0 is: 1

Sample Output 2:

Enter the base number: 2

Enter the base power: 10

2.0 to the power of 10.0 is: 1024.0

Sample Output 3:

Enter the base number: 5

Enter the base power: -2

5.0 to the power of -2.0 is: 0.04

Java Books for Beginners

Programming with Java : A Multimedia Approach by Radhika S. Grover (2011,…
Current Bid: $.99
Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: $50.80
Beginning Programming With Java for Dummies by Barry Burd (2012, Paperback)
Current Bid: $20.76

Java Video Tutorials

3.2″ Touch Screen 32G Quad Band Java Mobile Cell Phone Bluetooth FM, Pic/Video
Current Bid: $49.99
Motorola V620 Unlocked Bluetooth Video Camera JAVA Bluetooth Cell Phone Mint
Current Bid: $24.99

Other Java Source Code Examples

Java Simple Codes for Beginners
Java source codes on outputting Asterisk in Different Forms Using Recursion
Java Source Code: A Recursive Asterisk Diamond Shape
Java Source codes on Adding numbers inside the Array Using Recursion
Java Source code on Adding Numbers inside the Array using For Loop
Java Source Code: How to Sort Numbers in Bubble Sort Using Recursion
Java Source Code: Sort Numbers using Selection Sort
Java Source Code: Binary Search in Recursion
Java Source Code on Linear Search Using Recursion
Java Source code: How to Print a String Backward using Recursion
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source Code: How to make a Program that will determine a Person’s Salutation and Current Age
Java Source Code: Recursive Snow Flakes

PHP Code Sample Output on While Loop

PHP Code Sample Output on While Loop

PHP code, sample PHP Scripts, PHP code example on while loop

Another PHP code example for php programming. Loop in php is similar also to other programming languages. This hub will contain a sample code and a sample output on how while loop works in PHP programming.

Note.: before you continue, if you wish to learn more about php, you should know at a least, how to run a php file on your browser. If you do not know how it us done, please refer to the link below.

How to run a PHP code on your own browser using XAAMP and Notepad++

Please save the following code as whileloop.php, to avoid confusion on running this code.

PHP Code Example on While Loop

Sample output for whileloop.php

PHP Video Tutorials

“Learn the PHP basics from 17 info packed video tutorials.” “On CD”
Current Bid: $1.99
PHP Programming Video Training tutorials CBT – 7+ Hrs
Current Bid: $4.99
Learn php -mysql and web develop video &docs tutorials
Current Bid: $5.99

A Beginners’ Guide on Running the code:

1. Install XAAMP and Notepad++.

2. Open XAAMP and run the Apache as well as MY SQL on the XAAMP interface.

3. Copy and paste the “php code example in while loop” in notepad++ and save on the drive where you installed the Xaamp, then look for the Xaamp folder, open it, next open the htdocs folder and php file as whileloop.php.

4. After doing that, go to your browser and type this in the address bar: localhost/whileloop.php, the sample output should be similar as the output above.

Protector Glasses for All Computer Users

“Gunner” MATTE BLACK VIDEO GAME GLASSES computer gaming mw3 ps3 sunglasses
Current Bid: $6.95
Gunnar Optiks Edge Advanced Computer Gaming Eyewear Glasses Onyx Brand New
Current Bid: $59.98
Computer reading glasses protect your eyes from LCD LED screens tv’s and games
Current Bid: $11.99

Other PHP Code Examples and Tutorials

How to become a PHP Developer: Learn and Earn
Steps on How to Program in PHP: A Beginner’s Guide on PHP Programming-Part 1
How to run PHP Code or PHP Scripts and See the Result on Your Browser
HTML Basics: Learn The Basics of HTML and Create Your Own Webpage on Your Browser
PHP Sample Codes On Function:How to make Function in PHP
PHP Code Example on Associative Array and Multidimensional Array
PHP Code Example on Numeric Array
PHP Code Example on Switch Statement:Making Choice and Cases
PHP code Example for if statement
PHP Code Example on if else-if Statement
PHP Code Example on Do-while Loop
PHP Code Example on Foreach Loop and For Loop

Code For BufferedReader Class Example in Java

Code For BufferedReader Class Example in Java
Apple iPhone 3G – 8GB – Black (Factory Unlocked) Smartphone- Fully Functional!
Current Bid: $80.00

How to use BufferedReader?

One of the uses of BufferedReader in Java is to get the value of the variable input by the user, very much like what scanner does. The class BufferedReader should be also associated with an object. This object would be used to access the class BufferedReader. This is how we declare the bufferedReader with its own object:

BufferedReader putAnObjectHere = new BufferedReader(new InputStreamReader(System.in));

After that, you can import the packages associated with the declaration above. There are actually 2 packages that you need to import. These packages are:

import java.io.BufferedReader;

import java.io.InputStreamReader;

If you use netbeans, simply click the red error sign then click the first choice, do that twice. This is to automatically import the 2 packages above your main program and below the program’s package name.

Using the BufferedReader object on getting the integer input

To get the integer input by the user, simply declare a variable then access the BufferedReader class. The variable should be also declared in a String data type then parse it to integer in order to work. When I tried directly to declare an int, then print it on the screen, it doesn’t work correctly. See the example below on how to do it.

Apple iPod nano 7th Generation Blue (16 GB) (Latest Model)
Current Bid: $113.49
Apple iPod touch 5th Generation Black & Slate (32 GB) (Latest Model)
Current Bid: $248.89

String inputBytheUser = input.readLine();

int parseInputBytheUser = Integer.parseInt(inputBytheUser);

Unlike the class Scanner, BufferedReader always need other packages to be imported before you can use it. In the first declaration above, you still need to use the IOException package in order for the code to work. This is how to import the exception:

import java.io.IOException;

If it is a little cloudy, you can refer to the code below for a better example.

NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96

Using the BufferedReader object on getting the String input

The highly use data types for beginners are always integer and string, so in this hub, we will also show you on how get the input of the user on the string data type. This is how we will do it:

String StringInput = input.readLine();

If you already import a package for IOException, no need to import for this, it will work

Sample Java Program for BufferedReader

package bufferedreader_in_java;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Input a String: “);
String StringInput = input.readLine();
System.out.println();
System.out.print(“Input an Integer: “);
String integerInput = input.readLine();
int ParseIntegerInput = Integer.parseInt(integerInput);
System.out.println();
System.out.println(“The Integer Input: ” + ParseIntegerInput);
System.out.println(“The String Input: ” + StringInput);
}
}

Java Books

Head First Java, 2nd Edition, Kathy Sierra, Bert Bates, Sierra Kathy, Bates Be
Current Bid: $19.14
Data Structures and Algorithm Analysis in Java by Mark A. Weiss 3rd ,3e 3ed
Current Bid: $46.48

Related Java Tutorials

5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples

Java Video Tutorial

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99

Other Java Source Codes

Java Program: How to Use If Statement in Java Programming
Java Program: How to Use If-Else Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java Program: How to Use Switch Statement in Java
Java Program: How to Parse a String into Integer in Java Programming
Java Program: Count the Number of String Characters in Java
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Program: Reverse String in Java Using For Loop
Java Program: Palindrome Test Java Source Code
Java Program: How to Sort Numbers in an Array in a Descending Order
Java Program: How to Sort Numbers in Ascending Order
Sample Java Program for String Tokenizer
Sample Program for JOptionpane and Java Source Code for Basic Calculator
What is a Java Scanner Class and How to Use it?
What is MDAS? Sample Java Source Code for MDAS
What is a Static Method in Java?
Casting in Java

What is a Static Method in Java?

What is a Static Method in Java?
NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96

Static Method in Java

Making your method static means that you do not need to instantiate an object in order to access that method of a certain class. It can be directly accessible to the main program by simply calling the class name and the class method.

How to have static method in Java?

To make a static method, just add a ‘static’ word before the data type of the method name. For example:

public static int methodname();

How to call the Static Method in the main Program?

You can call the static method in the main program like this:

className.methodName();

It also works if you declare an object for the class to access the method but that is discourage, because in the first place static is considered as a short cut on accessing the method of the class even without the object declaration (though in my other examples here with a static method, I still declared an object for the methods, it’s your turn to make the practice correct, I shall mind this practice in my future codes).

See Also:

How to Run Java Code Online

Like always, you can see how to use it, and how it behaves by providing a complete running program for it. For the sample java program on Static method, see the source code and the program algorithm below.

Unlocked Samsung i777 Galaxy S 2 II AT&T Android 16GB Black Camera Cell Phone
Current Bid: $179.95

Program Algorithm:

The program is just simple, we will just show how this static method works. So, we will just output a string inside a method. If you have a program in mind, try using static method and access it directly in your main program.

Java Source Code for Static Method

//main class
package static_method_in_java;
public class Main {
public static void main(String[] args) {
Static_Method.sample_static_method();
}
}
//java class
package static_method_in_java;
public class Static_Method {
public static void sample_static_method()
{
System.out.println(“This is how Static Method works. Simple isn’t it? :)”);
System.out.println(“Aisha91 at your Service. Bow! LoL, just kidding.”);
}
}

Sample Output:

Related Java Tutorials:

5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples

Java Video Tutorials

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99

Java Book

Head First Java, 2nd Edition, Kathy Sierra, Bert Bates, Sierra Kathy, Bates Be
Current Bid: $19.14
Programming with Java : A Multimedia Approach by Radhika S. Grover (2011,…
Current Bid: $.99

Other Sample Java Codes

Java Program: How to Use If Statement in Java Programming
Java Program: How to Use If-Else Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java Program: How to Use Switch Statement in Java
Java source code in Recursive function for X to the power Y
Java Program: How to Parse a String into Integer in Java Programming
Java Program: Count the Number of String Characters in Java
Java Program: Reverse String in Java Using For Loop
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Program: Palindrome Test Java Source Code
Java Program: How to Sort Numbers in an Array in a Descending Order
Java Program: How to Sort Numbers in Ascending Order
Sample Java Program for String Tokenizer
Sample Program for JOptionpane and Java Source Code for Basic Calculator
What is a Java Scanner Class and How to Use it?
What is MDAS? Sample Java Source Code for MDAS

Class in Java: Learn More about Java Classes with Sample Java Codes

Class in Java: Learn More about Java Classes with Sample Java Codes
3.2″ Touch Screen 32G Quad Band Java Mobile Cell Phone Bluetooth FM, Pic/Video
Current Bid: $49.99
Motorola V620 Unlocked Bluetooth Video Camera JAVA Bluetooth Cell Phone Mint
Current Bid: $24.99

What to learn about Java Classes? Questions to be answered in this hub.

1. How to make a Java Class?

2. How to access a Java Class?

3. What is a method or a Function?

4. What are the method or function parameters?

5. How to access a method by another method of the same class?

6. What is a Static class?

7. How to overload a method or function Name?

8. What are constructors (constructor, empty constructor, copy constructor)?

9. How to overload a constructor?

How to make a Java Class?

Answer: Make a New Project. Click the Project Source Packages, right click the project name folder,mouse Over New and click Java Class. If you do not have any idea on how to make a new project in Java, here is a tutorial on how to make a new project in Java using netbeans editor.

How to Access a Java Class?

Answer: To access a Java Class and its methods or functions you must declare an object under the java class name you want to access.

Syntax:

ClassNameYouWantToAccess objectName = new ClassNameYouWantToAccess();
//this is how you instantiate an object to access a certain class
objectName.ChosenMethodInTheClassYouWantToAccess();
//this is how the object can access the public method of the class

Java Books

Programming with Java : A Multimedia Approach by Radhika S. Grover (2011,…
Current Bid: $.99
Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: $50.80
Beginning Programming With Java for Dummies by Barry Burd (2012, Paperback)
Current Bid: $20.76
Java Network Programming by Elliote Rusty Harold and Elliotte Rusty Harold…
Current Bid: $.99

For Java Source Code Example: See the Java Source Code of Binary Search and read through comments.

What is a method or a function?

Answer: A method or a function is a block of code inside a class that is intended to perform a specific task. The Binary Search in the above example has an example of a method. Please check it out again and read the comment to know more about a class method or function.

What is a Method or Function Parameters?

Answer: Parameters are variables declared inside a method or class. The example of a method with its parameters in binary search is this:

public int binSearch(int[] arr, int fIndex, int lIndex,int search)
//int[] arr, int fIndex, int lIndex,int search are the parameters of the method binSearch.

Related Hubs

Java Tutorial Examples
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Simple Codes for Beginners
How to Program in Java: Complete Simple Easy Steps
Basic Knowledge Required in Programming
5 Important Tips to Learn Java Programming and Other Programming Languages

How to Access a Method to Another Method of the Same Class?

Answer: Just copy the method name and put the same type and number of parameters. In accessing a method with another method of the same class, there is no need to declare an object. To see how it is done, See this Java Source Code on Diamond Asterisk Shape and read through comments.

What is Static in a Class?

If you use a static type in a public method of a class, you do not need to declare an object to access that method in the main class. Refer to the next java source code example.

Syntax for a static and Syntax for accessing static method.

public static dataType methodName()//this how to make a static method
{
}
className.staticMethodName(); //this is how to access a static method

How to Overload a Method or a Function Name?

Answer: Overloading a method or a function is just making a method/function with the same name as the other. However to overload them, they must have a different parameters.

Java Source Code Example:

//java class
package samlejava;
class javaClass {
public String Schedule(String room, String section)//method1
{
return room +” “+” “+ section + ” “+” “;
}
public String Schedule(String subject,String day, String time)//overloaded methodName
{
return subject +” “+” “+day+ ” “+” “+ time+” “+” “;
}
public static String Schedule(String teacher)//overloaded methodName with a type static
{
return teacher;
}
}
//main class
package samlejava;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner readInput = new Scanner(System.in);
System.out.print(“Enter your Name: “);
String name= readInput.nextLine();
System.out.print(“Add a Subject: “);
String subject= readInput.nextLine();
System.out.print(“Add a Section: “);
String section= readInput.nextLine();
System.out.print(“Add a Room for the Section: “);
String room= readInput.nextLine();
System.out.print(“Add Day for the Subject: “);
String day= readInput.nextLine();
System.out.print(“Add time for Subject: “);
String time= readInput.nextLine();
System.out.print(“Add teacher for the Subject: “);
String teacher= readInput.nextLine();
javaClass iAccess = new javaClass();//access javaClass class,instamtiate object
System.out.println();
System.out.println(name + “Here is a new Subject Schedule for You!”);
System.out.println();
System.out.println(“Room” + ” ” + ” ” + “Section” + ” ” +” ” +
“Subject” + ” ” +” ” + “Day” + ” “+” ” +
“TIme” + ” ” + ” ” + “Teacher” );
System.out.println();
System.out.print (iAccess.Schedule(room, section));//access method1
System.out.print (iAccess.Schedule(subject, day, time));//access method2
System.out.print (javaClass.Schedule(teacher));//this is how static works
}
}

Sample Output:

See all 2 photos
Sample Output

What are constructors (Normal Constructor, Copy Constructor, Empty or default Constructor)?

Answer: Constructors are those which are similar to the class name. Basically, constructors are used to initialize the variables. They do not have any data types.

Syntax for constructor:

public className(list of parameters here)
{
initialization here.
}

Constructors can be 1 or more in a class, however they should have different parameters. Empty or default constructor is always present in a class. You can make your own empty constructor or let the computer generate it automatically for you, as you execute the class. You can tell that the constructor is default if it do not have any formal parameters. Moreover, copy constructor, is a constructor which copy the task of an existing method.

Syntax for copy constructor:

public className(className Parameter)
{
copy code
}

How to Overload a Constructor?

Answer: Overloading a constructor is also just like overloading a function name with different formal parameters.

To understand about constructor more: Refer to the Java Source Code below and Read through the comments:

//java class
package constructor;
public class constructor {
String fName;
String lName;
String gender;
int age;
public constructor() //empty constructor initialized variables
{
fName= ” “;
lName= ” “;
gender = ” “;
age= 0;
}
public constructor(String firstName, String lastName)//constructor1 return fName and lName
{
fName = firstName;
lName = lastName;
System.out.print(firstName+ ” ” + lastName);
}
public constructor(String Sex, int Age)//overloading the constructor Name return gender & age
{
gender = Sex;
age = Age;
System.out.print( Sex+ ” : ” + Age);
}
public constructor(String firstName, String lastName,String Sex, int Age)//overloading 2 return all
{
fName = firstName;
lName = lastName;
gender = Sex;
age = Age;
System.out.print(firstName+ ” ” + lastName + “: ” + Sex+ ” : ” + Age +” yrs. old”);
}
public constructor(constructor copyCons)// copy the last constructor
{
fName = copyCons.fName;
lName = copyCons.lName;
gender = copyCons.gender;
age = copyCons.age;
System.out.print(fName+ ” ” + lName+ “: ” + gender+ ” : ” + age +” yrs. old”);
}
}
//main class
package constructor;
import java.util.Scanner; //import scanner for input Stream
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);//instantiate object to input
System.out.print(“Enter first name: “);
String fName = input.nextLine(); // read the string input
System.out.print(“Enter last name: “);
String lName = input.nextLine(); // read the string input
System.out.print(“Enter gender: “);
String gender = input.nextLine(); // read the string input
System.out.print(“Enter Age: “);
int age = input.nextInt(); // read the integer input
System.out.println(“RESULT FROM 3RD CONSTRUCTOR”);
constructor access1 = new constructor(fName, lName, gender, age); //accesslast constructor;
System.out.println();
System.out.println(“RESULT FROM COPY CONSTRUCTOR”);
constructor access2 = new constructor(access1); // access copy constructor
}
}
See all 2 photos
Sample Output

Cool Android Phones!

HTC Droid Incredible Verizon Wireless Wifi 8.0 MP Camera 8GB Android Cell Phone
Current Bid: $64.95
NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96
Unlocked 7 inch Phablet Android 4.0 ICS Smartphone Tablet Phonepad w/ Play Store
Current Bid: $55.25
NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT T T-Mobile
Current Bid: $63.96
Motorola Moto DROID A855 (Verizon Wireless) WiFi Phone 5MP Camera Clean ESN (B)
Current Bid: $21.01

Other Java Source Code Examples

What is a Java Scanner Class and How to Use it?
Java Program: How to Use Switch Statement in Java
Java source code Sample: Print Different Asterisk Shapes in Recursion
Java Source Code: A Recursive Asterisk Diamond Shape
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java Source Code: Sort Numbers in Selection Sort
Java Source Code sample: Sort Numbers in Bubble Sort
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source code sample: Print String Backward using Recursion
Java Source Code Recursion: Recursive Koch Snow Flakes
Java Source Code in a Recursive Linear Search
Java Source Code: Binary Search in Recursion
Java Source Code Determine Person’s Salutation and Current Age
Java source code: Recursive function for X to the power Y