answersLogoWhite

0

How do you add methods and variable in a java program?

Updated: 8/19/2019
User Avatar

NaveenGarg

Lvl 1
12y ago

Best Answer

For example, if I have a program like this:

import java.util.*;

public class Example

{

public static void main(String[] args)

{

System.out.println("Test");

Scanner in = new Scanner(System.in);

System.out.println("Please type a number.");

double d = in.nextDouble();

System.out.println("Please type another number.");

double e = in.nextDouble();

System.out.println("The two numbers multiplied together yields: " + (d*e));

System.out.println("The two numbers added together yields: " + (d+e));

System.out.println("The remainder of the first divided by the second yields: " + (d%e));

}

}

and I want to add a variable to it, anywhere within the main method (in between the inner-most pair of braces in this case, as we don't have any for loops, while loops, if statements, etc) I would type something like:

int q = 47;

which would create a integer-space in memory, name it q, and assign the value of 47 to it. To add methods is a bit more complex.

Methods are written like this:

public static void anotherMethod()

{

//Do something here

}

Now in order to use these, you would call them from your main method. To do so, you would insert code into your main method calling on the new method (method calling). Code for this would look like:

anotherMethod();

The line of code right above would call the method "anotherMethod" inside the same java class.

You would insert this method after the main method ends (so after the final brace of main) but inside the brace for the class. In this code sample, you would insert it before the final brace, like so:

import java.util.*;

public class Example

{

public static void main(String[] args)

{

System.out.println("Test");

Scanner in = new Scanner(System.in);

System.out.println("Please type a number.");

double d = in.nextDouble();

System.out.println("Please type another number.");

double e = in.nextDouble();

System.out.println("The two numbers multiplied together yields: " + (d*e));

System.out.println("The two numbers added together yields: " + (d+e));

System.out.println("The remainder of the first divided by the second yields: " + (d%e));

int q = 47;

anotherMethod();

}

public static void anotherMethod()

{

//Do something here

}

}

As you will see, in the above code I declared int q to be 47 (added that line in) and after the braces that defined the main method, I inserted another method called 'anotherMethod'. I did a method call from the main method with the line of code:

anotherMethod();

If you have any questions about the above explanation feel free to send me a PM on answers.com, or edit this question :)

EDIT: I have made a YouTube video for this question, with a link to it in the related links area.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you add methods and variable in a java program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


What is add method in a java program?

If the "add" is part of the Java Collections API then calling c.add(o) adds an object o to the given collection (list or set). Likewise, the AWT API has add methods to add components to a Frame or Panel.


How do you add a variable from a Java program into a database through an SQL command within the Java program?

depends on the database you're trying to insert into for sql specifics, but... http://www.exampledepot.com/egs/java.sql/Insert.html provides a very good oracle answer/example.


How do you write a program in java to read ten numbers and print sum of ten integers?

Add the numbers into one variable as you read them in. But if you prefer, you can read the numbers into an array and then use a loop to add the numbers together.


Write Four different java statement that each add to integer variable x?

++x; x++; x += 1; x = x + 1;


What are the precautions that once should take while writing program in java?

Use loops only when required and add proper loop termination conditionsDo not duplicate code. Try to re-use code as much as possibleAlways add comments to the methods


Can you declare an instance variable of class StringBuffer in Java program?

Here's how to create a StringBuffer instance.StringBuffer sb = new StringBuffer();sb.append("Add this is to the buffer");// ...Note the StringBuffer has synchronized methods so if it is only accessed in a single thread context then a StringBuilder is preferred. StringBuffer and StringBuilder both implement the Appendable and CharSequence interfaces so can be used interchangeably.


What is function overloading in java?

Any function or method in Java that is coded by the programmer is called a user defined method in Java. The JAVA API (Application Programming Interface) has a set of predefined classes & methods that are for our usage. Whatever methods we create apart from these are termed as user defined methods. In java we not use the term functions. We call them "Methods"


How do you add import packages to a java program in netbeans?

package thisPackage; import otherPackage.*; class myClass { }


How do you set java environment variables?

You need to set The CLASSPATH variable which is an argument set on the command-line that tells the Java Virtual Machine where to look for user-defined classes and packages in Java programs. Syntax is java -classpath "path to the packages". Set the PATH variable if you want to be able to conveniently run the Java 2 SDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command. To set the PATH permanently, add the full path of the j2sdk1.4.2_version\bin directory to the PATH variable. -sravyaa


How do you find the average of rows and columns in a 2d array in java?

You add up all the array elements, then divide by the number of elements. You can use a nested for() loop in Java; inside the inner for() loop, you can both increase a counter (to count how many elements there are), and add to a "sum" variable.


How do you change the current directory to 'etcjavabin' and append this path to existing PATH settings?

To change the current directory to /etc/java/bin, use the command "cd /etc/java/bin".To add the above path to the PATH variable, type export PATH="$PATH:/etc/java/bin".To check whether you have successfully added that PATH, type "echo $PATH".To permanently add /etc/java/bin to PATH variable, edit /etc/profile or ~/.bashrc file and add the command, export PATH="$PATH:/etc/java/bin". (NOTE: After adding, you will need to reboot the machine or type "source /etc/profile" or "source ~/.bashrc".