Java - call void function that print my name - java

public class Main {
public static void main(String[] args) {
**how i call the function here** ?
}//public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}//main

package com.pervacio.adminportal.tradein.constants;
public class Main {
public static void main(String[] args) {
nameAndAge();
}//public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}//main
Please read this for better understanding: Java Methods | Tutorials Point

If the function that has to be called is in the same class then just call the function like this:
function_name(parameters);
But if the function is in another class then you will have to create an object of the class and call:
Class_name object_name=new Class_name(); object_name.function_name();

These are the ways to call the function :
public class Main {
public static void main(String[] args) {
nameAndAge();
// or
Main.nameAndAge();
// or
Main s=new Main();
s.nameAndAge();
} //public
public static void nameAndAge() {
System.out.println("Name: Rubik Seviyants Age: 27");
}
}

Related

java error:constructor High in class High cannot be applied to given types. why this error comes?

class High
{
public High(String s)
{
System.out.print("From High");
}
}
public class Main extends High
{
public Main(String s)
{
System.out.print("From Low");
}
public static void main (String[] args) {
Main m=new Main("HighLow");
System.out.println(" ");
}
}
I am not really sure what you are trying to do with your code, but the problem is that in your Main constructor you are not explicitly calling the High constructor (since you are extending it you must). When you ommit the explicit call, the compiler will add it for you, but using the no-argument constructor, which does not exist in High. The consequence is that you must add the call on your own as follows:
public class Main extends High {
public Main(String s) {
super(s); // This is the line you are missing
System.out.print("From Low");
}
public static void main(String[] args) {
Main m = new Main("HighLow");
System.out.println(" ");
}
}

can any body tell me why static block is called when i am calling the static variable only

package hello;
class hellogy {
static
{
System.out.println("i will obviously enter hera");
}
public static void main(String[] args) {
System.out.println( dls.i);
}
}
class dls
{
static int i=10;
static
{
System.out.println("i will obvioussadsfaasly enter hera");
}
}
Static block in Java is executed before main method. If we declare a static block in java class it is executed when class loads.

Getting error referencing an object from a different class in Java

I've been working with Java in the last few days, so I am very new to programming in Java.
I am currently going through a bunch of online tutorials and trying to learn as much as possible. In one tutorial, we are getting to learn how to use objects correctly. The person who made the video uses an online compiler, while i follow along in eclipse.
In my code I have two classes
The first is called objectDesign
public class objectDesign {
public static void main(String[] args) {
System.out.println("We are creating a new PEZ dispenser");
PezDispenser dispenser = new PezDispenser();
System.out.printf("The dispenser is %s", dispenser.characterName);
}
}
The second is called PezDispenser
public class PezDispenser {
public String characterName;
public static void main(String[] args) {
String characterName="Mario";
}
}
The goal is to define the character who the object is supposed to be. I used a string called characterName and set it to Mario which i would like to return when I run the objectDesign class. I made the string public thinking that would enable the objectDesign class to find the information. However the console returns "The dispenser is null" every time I run the code.
What am I doing wrong?
You can use below code
package com.stackoverflow;
public class ObjectDesign {
public static void main(String[] args) {
System.out.println("We are creating a new PEZ dispenser");
PezDispenser dispenser = new PezDispenser("Mario");
System.out.printf("The dispenser is %s", dispenser.characterName);
}
}
package com.stackoverflow;
public class PezDispenser {
public String characterName;
public PezDispenser(String characterName) {
// TODO Auto-generated constructor stub
this.characterName=characterName;
}
}
here are few ways of doing this
Option 1:
public class PezDispenser {
public String characterName = "Mario";
public static void main(String[] args) {
String characterName="Mario"; // This statement is never executed because this class's main was never invoked.
}
}
Option 2:
public class PezDispenser {
public String characterName;
public static void main(String[] args) {
String characterName="Mario";
}
}
public class objectDesign {
public static void main(String[] args) {
System.out.println("We are creating a new PEZ dispenser");
PezDispenser dispenser = new PezDispenser();
dispenser.characterName = "Mario";
System.out.printf("The dispenser is %s", dispenser.characterName);
}
}
These are not the best options, but going by what you are trying to achieve these should serve the purpose.

How to call a behavioral method from main method in 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();
}

Nesting calls to static methods

I want to do something like below, but it does not work. My objective is to be able to nest function calls to static helper classes to get more brevity.
public class StaticHelper {
public static Class<StaticHelper> doSomthing() {
System.out.println("I just did something !!");
return StaticHelper.class;
}
public static Class<StaticHelper> doSomthingElse() {
System.out.println("I just did something else !!");
return StaticHelper.class;
}
public static void main(String[] args) {
// Does not compiles
StaticHelper.doSomthing().doSomthingElse();
}
}
Is this possible? If so a simple example as above will be very helpful.
I guess you want something like this.
public class StaticHelper {
private final static StaticHelper INSTANCE = new StaticHelper();
public static StaticHelper doSomthing(){
System.out.println("I just did something !!");
return INSTANCE;
}
public static StaticHelper doSomthingElse(){
System.out.println("I just did something else !!");
return INSTANCE;
}
public static void main(String[] args) {
StaticHelper.doSomthing().doSomthingElse();
}
}
or another way
public class StaticHelper {
public static SomeClass doSomthing(){
return new SomeClass().doSomthing();
}
public static SomeClass doSomthingElse(){
return new SomeClass().doSomthingElse();
}
public static void main(String[] args) {
StaticHelper.doSomthing().doSomthingElse();
}
private static class SomeClass {
public SomeClass doSomthing(){
System.out.println("I just did something !!");
return this;
}
public SomeClass doSomthingElse(){
System.out.println("I just did something else !!");
return this;
}
}
}
That is not possible, but using static imports is.
public class StaticHelper {
public static void doSomething() {
System.out.println("I just did something !!");
}
public static void doSomethingElse() {
System.out.println("I just did something else !!");
}
}
in another class:
import static StaticHelper.*;
class Other {
public static void main(String[] args) {
doSomething(); // calls static methods from StaticHelper
doSomethingElse();
}
}
or - if the methods are logically connected - you can have one static factory method and the rest are instance methods:
public class StaticHelper {
public static void beginDoingSomething() {
// static factory method - you can pass parameters to it if needed
System.out.println("I just did something !!");
return new StaticHelper();
// if needed, initialize the instance with the parameters
}
public StaticHelper andDoSomethingElse() {
// instance method
// can use the instance parameters (passed to the constructor in the static factory method)
// or use parameters passed to this method
System.out.println("I just did something else !!");
return this;
// returns this for chaining
}
}
in another class:
import static StaticHelper.*;
class Other {
public static void main(String[] args) {
doSomething().andDoSomethingElse().andDoSomethingElse();
}
}
If you name the methods nicely, you can form a sentence:
validate(object).checkEmail().checkName().checkTelephoneStartsWith("+11");
where validate(object) is a static factory method constructing a new validator instance for the given object.

Categories

Resources