How to call a behavioral method from main method in java? - java

I have a non static method abc() in the class that contains main() method. Now how to call this method abc() from main. Can i create an instance of the class in the same class?
public class A
{
public static void main(String[] args)
{
//how to call abc() here?
}
private int abc()
{
return 2;
}
}

You have to instanciate current class, use this code:
public static void main(String[] args)
{
A a = new A();
int value = a.abc();
}

Related

Calling private method inside private class inside Inner class

Need to call a private method foo() of the class Inner.Private, where Private is an inner private class from the main method of the main class.
The code is something like this:
public class MainClass {
public static void main(String[] args) throws Exception {
// Need to invoke foo() from here
}
static class Inner {
private class Private {
private String foo() {
return "someString";
}
}
}
}
I was trying to get this using Java Reflection, but I am facing issues from this approach.
My attempt to invoke the foo() is:
Inner innerClassObject = new Inner();
Method method = Inner.Private.class.getDeclaredMethod("foo");
method.setAccessible(true);
method.invoke(innerClassObject);
But this gives a NoSuchMethodException:
Exception in thread "main" java.lang.NoSuchMethodException:
default.MainClass$Inner$Private.foo()
at java.lang.Class.getDeclaredMethod(Unknown Source)
I am stuck at this point, is this achievable by Java Reflection, or any other way?
Ummm... why not simply new Inner().new Private().foo()?
Why are you doing this
Inner.Private.class
Instead of
innerClassObject.getClass()
For e.x:
public class Test {
private int foo(){
System.out.println("Test");
return 1;
}
public static void main(String [] args) throws InterruptedException,
NoSuchMethodException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException
{
Test innerClassObject = new Test();
Method method =
innerClassObject.getClass().getDeclaredMethod("foo",null);
method.setAccessible(true);
method.invoke(innerClassObject);
}
}
Why waste an instantiation just to call a method as was described? You will inevitably want to save various instances for latter use as the classes develop.
public class MainClass {
public static void main(String[] args) throws Exception {
// Need to invoke foo() from here
Inner inner = new Inner();
Inner.Private pvt = inner.new Private();
System.out.println(pvt.foo());
}
static class Inner {
private class Private {
private String foo() {
return "someString";
}
}
}
}
Prints
someString

How do I call a method that has a variable passed through it?

I'm trying to call a method from within another method. I'm understanding this simply enough, until one of those methods needs a variable carried through, and then nothing I try works.
I know that I could do this in one method, but my coursework needs me to lay it out in such a way. Why doesn't this work?
public class test2 {
public static void testMethod() {
int randomNumber = 1;
}
public static void anotherTestMethod(int randomNumber) {
System.out.println(randomNumber);
}
public static void main(String[] args) {
anotherTestMethod();
}
}
You are calling a method that has an int parameter in its signature. You should pass that parameter when calling the method. I think you are trying to use a global variable, in that case, you should declare it outside any method, as a part of the class.
public class test2 {
public static int testMethod() {
int randomNumber = 1;
return randomNumber;
}
public static void anotherTestMethod() {
System.out.println(testMethod());
}
public static void main(String[] args) {
anotherTestMethod();
}
}

Is it possible to get terminal input in init()?

I have this code:
public class test {
init() {
//How can i get access of args?
}
public static void main(String[] args) {
new test();
}
How can I get access of "args" in init()?
Just pass it as an argument.
public class test{
init(String[] args){
//How can i get access of args?
}
public static void main(String[] args) {
test t = new test();
t.init(args);
}
}
Remember, you should capitalise your class names.
Define an instance field within the class, and create constructor to accept the array and set the field:
public class test{
String[] args;
public test(String[] args){
this.args = args;
}
init(){
}
}
public static void main(String[] args) {
new test(args);
}
Or pass it as an argument to the method.
init(String[] args){
}
If init requires anything within the test class, you must create an instance of the class
test t = new test();
t.init(args);
If init does not require a new instance of test (eg doesn't access any instance variables or methods) you could define init as static and directly call the method:
static init(String[] args){//method declaration
}
public static void main(String[] args) {
test.init(args);//call the method in a static way
}

Variable Accessible by All Methods Without Parameters?

Is it possible to have a "global" variable, i.e., "balance", which all methods can access without parameters?
Something like:
public static void main(String[] args{
makevariablehere
}
Could be called in another method:
public static int someMethod() {
variable = newVariable;
}
You can define it as a static field on the class. See the example below, which stores the number of args passed to the main method in a static field, so that it may be returned by the getNumberOfArgs() method.
public class MyClass {
private static int argCount;
public static void main(String[] args) {
argCount = args.length;
}
public static int getNumberOfArgs() {
return argCount;
}
}

Error of static and non-static

I'm confused between when to use public static void xxx() and public void xxx(). The error I'm getting is in the main class.
generateRandomNumber();
The error : (error: non-static method generateRandomNumber() cannot be
referenced from a static context)
getUserInput();
he error : (error: non-static method getUserInput() cannot be
referenced from a static context)
getResult();
The error : (error: non-static method getResult() cannot be referenced
from a static context)
public class HighLowGame {
int randomNumber;
int guess;
public void generateRandomNumber(){
randomNumber = (0+(int)(Math.random() * ((0 - 99) + 1)));
}
public void getUserInput(){
guess = Integer.parseInt(JOptionPane.showInputDialog(
null,
"Plaese input"));
}
public String getResult(){
if(randomNumber<guess){
return "Your number is bigger than magic number";
}
else if (randomNumber>guess) {
return "Your number is smaller than magic number";
}
else {
return "You are correct! "+randomNumber;
}
}
public static void main(String[] args){
generateRandomNumber();
getUserInput();
getResult();
}
}
you cannot access a non-static instance method/variable from static context directly. you will need an instance of your class to access them .
public static void main(String[] args){
HighLowGame ref = new HighLowGame ();
ref. generateRandomNumber();
ref.getUserInput();
ref.getResult();
}
or you can make all these methods static and access them directly. make methods static when you think all the instances of that class should share the date/methods.
You're attempting to call a method which needs an instance of HighLowGame to work.
Change your main method to:-
public static void main(String[] args){
HighLowGame game = new HighLowGame();
game.generateRandomNumber();
game.getUserInput();
game.getResult();
}
You have to call the Methods on an instance of HighLowGame.
public static void main(String[] args){
HighLowGame game = new HighLowGame();
game.generateRandomNumber();
game.getUserInput();
game.getResult();
}
or declare your Methods static like
public static void generateRandomNumber(){ ..}

Categories

Resources