Simple Java OOP Class Error - java

I'm trying to print my name using Java OOP Classes, Objects and Method. Below are the two scripts I'm using. Nothing is showing on the screen. I'm using Netbeans IDE.
package name;
public class Begin {
public static void displayName(){
System.out.print("Say My Name");
}
}
===========================
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin.displayName();
}
}

You're missing the class in your "Useaname" class declaration.
public class Useaname {

Where is Your Keyword class before your Username ?? :)
public class Useaname {
public static void main(String[] args) {
Begin.displayName();
}
}

You have to declare the method static in order to call it like Begin.displayName().
public static void displayName() {
This tells makes the method a function of the class as opposed to any particular instance of the class.
Alternatively, you can instantiate an object to call the method:
Begin begin = new Begin();
begin.displayName();

You have missed the keyword class before the class name Useaname. Here is the solution code.
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args) {
Begin.displayName();
}
}

This code should not compile.
Begin.displayName(); tries to invoke static method - you dont have such a thing :).
you have to ways - create new Begin object and invoke method on it - new Begin().displayName()
or declare displayName as static method.

There are at least two solutions to this problem:
(1) You can make the displayName() method static. This will allow you to call it with the class name.
public static void displayName() {
// snip
}
(2) You can create an object from your Begin class in order to call the non-static member method:
public static void main(String[] args){
Begin b = new Begin();
b.displayName();
}
To summarize, you should learn about objects and object creation as a beginning step to understanding Java and OOP in general.

You could use a static modifier on your displayName() method or create a new instance of your Begin class:
package usename;
public class Useaname {
public static void main(String[] args){
Begin begin = new Begin();
begin.displayName();
}
}

You haven't use the keyword "class" before "usename"
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
Correction
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args){
Begin.displayName();
}
}

you forgot class keyword ! and you can create object for class Begin
import name.Begin;
public class Usaname
{
public static void main(String []args)
{
Begin ob = new Begin();
ob.displayName();
}
}

You forgot to add class before Username
package username;
import name.Begin;
public class username{
public static void main(String[] args){
Begin.displayName();
}
}

What you are missing here is the class keyword before Username class declaration
Since you declared the displayName() method as static using the static access modifier , there is no need to create an object of the Begin class in order to invoke the displayName() method.
In general , this is how the Username class should be :
package usename;
import name.Begin;
public class Useaname {
public static void main(String[] args){
Begin.displayName();
}
}
Alternatively, you can invoke the displayName() using a class instance by creating an object of the Begin class, just like this:
package usename;
import name.Begin;
public Useaname {
public static void main(String[] args){
Begin b = new Begin();
b.displayName();
}
}

Related

How a static method call works in Java?

When calling out the function testFunc(), I am not using the syntax
Apples.testFunc(). Yet the code runs successfully. How so?
class Apples {
public static void main(String args[] ) {
testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
Since, the static method is in the same class. So, you don't need to specify the class name.
If it is in different class, then you need to specify the class name.
Remember: Non-static methods can access both static and non-static members whereas static methods can access only static members.
For example :
Calling a static method present in different class, you need to do like this :
import com.example.Test;
public class Apples {
public static void main(String args[]) {
Test.testFunc();
}
}
package com.example;
public class Test {
public static void testFunc() {
System.out.println("Hello world!");
}
}
Your function testFunc() is in same class where the function main is. So you don't need to use class name before function testFunc().
When the memory is allocated for the class, Static method or static variable is assigned memory within a class. So we can access static member function or data variable using class name.
we can call static function as below
class Main1 {
public static void main(String args[] ) {
Main1.testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
or
class Main1 {
public static void main(String args[] ) {
testFunc();
}
public static void testFunc() {
System.out.println("Hello world!");
}
}
the answer will be same, but when static function is in other class then we must use classname to call it

Java moving Main class reference

I was wondering if it was a bad way of programming to move the main reference in the subclass of the software.
It will probably be hard to understand what i really mean so i will do an example.
public class Main{
public static void main(String[] args) {
Main app = new Main();
Toto myToto = new Toto();
myToto.something(app);
}
}
public class Toto{
public void something(Main app){
}
}
"this" is a non-static variable, so it can not be referenced from a static method.
so we can not call something() by using "this" keyword.
Try the below code, it is working correctly
class Main{
public static void main(String[] args) {
Toto myToto = new Toto();
Main m= new Main();
myToto.something(m);
}
}
class Toto{
public void something(Main app){
}
}

How to handle class's from Two Different .Java Files?

I have two java class file's in my project.
First one is main.Java
Second one is function.java
How to call function.java method from main.java
for Example.
main.Java
public class main {
//call function here
}
function.Java
public class function {
public void example(){
System.out.println("Function working");
}
}
How to call function.java example method from Main.Java?
Class name should be always start with Upper letter.
This is the way to call a function in another class in another .java
In Main.java
public class Main{
public static void main(String[] args){
SecondClass sc = new SecondClass();
sc.go();
}
}
In SecondClass.java
public class SecondClass{
public void go(){
System.out.println("Done");
}
}
If you want to call the method in the same class
public class Main{
public void go(){
System.out.println("Done");
}
public static void main(String[] args){
Main m = new Main();
m.go();
}
}
or this
public class Main{
public static void go(){
System.out.println("Done");
}
public static void main(String[] args){
go();
}
}
First make the example() method static
public static void example(){
System.out.println("Function working");
}
Then call
public class main {
//call function here
public static void main(String[] args) {
function.example();
}
}
if they are not in the same package you should import function.java in main.java like this
import function
then you should create an instance of function class like this where you want to use it
function func = new function();
then you can call the example method using func object you created like this
func.example();
Your method isn't static, so you need to instantiate object from this class(function) to obtain access to the example method. First of all read some java specific book, so you can understand what static keyword is and how you can use it.
The code below will work :
package main.Java; //this is from where you want to call.
public class function {
public void inFunction(){
function.Java.function function = new function.Java.function();
function.example();
}
}
Happy learning :)

Access static variable from a different class

So I have two classes:
A Main class with the public static void main(String args[]) method
and a Voice class that accesses a static variable from that class.
Within the main class are methods that are used by itself, and are required to be static along with some of its variables.
So I have a static variable within the Main class (that's created/filled in the public static void main(String args[]) method. That's why this case is special) which the other class should be able to access.
Here is an example of what's happening:
public class Main(){
public static int variable;
/*
Unrelated methods go here.
*/
public static void main(String args[]){
Voice v = new Voice();//This is just here for the code to make sense.
variable = 5;
v.doSomething();
}
}
public class Voice(){
public void doSomething(){
System.out.println(Main.variable);
}
}
Upon calling the doSomething() method in Voice, it leads to a nullPointerException.
I could fix this by passing on the variable variable to the Voice class, but is there a more easy way to fix this in the long run, if for instance, I needed to use more than one static variable from the Main class?
Your code is having syntax error. You can use this
class Main{
public static int variable;
/*
Unrelated methods go here.
*/
public static void main(String args[]){
Voice v = new Voice();//This is just here for the code to make sense.
variable = 5;
v.doSomething();
}
}
class Voice{
public void doSomething(){
System.out.println(Main.variable);
}
}
Output will be 5
You should do as follows
public class Main{
public static int variable;
/*
Unrelated methods go here.
*/
public static void main(String args[]){
Voice v = new Voice();//This is just here for the code to make sense.
variable = 5;
v.doSomething();
}
}
class Voice{
public void doSomething(){
System.out.println(Main.variable);
}
}

why can't I create a method in the main method [duplicate]

This question already has answers here:
Writing a function inside the main method - Java
(7 answers)
Closed 6 years ago.
Hello I am a want create method into main?
So this is code where i want crete method:
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}}
Java does not allow to create methods within a method. This is a general rule and NOT specific to main method.
Perhaps what you are looking for is to create additional static methods in your class:
public final class MyCommand {
private MyCommand() {
//
}
private static void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
releaseTheCatsOfWar();
}
}
Of course, it would be better to do:
public final class MyCommand {
private MyCommand() {
//
}
private void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
MyCommand that = new MyCommand();
that.releaseTheCatsOfWar();
}
}
you cannot create methods within methods in Java.
You can have it as an additional method of your class and call it form main.
public static void main is not a class, it's a method within class. And your class is input (by the way, you should start class name with an uppercase letter, like Input), in which you define method main, from which your program starts working.
Although it is entirely pointless, the closest way you would get to achieving your results is with the following:
public class Main {
private static interface MethodMaker{
void randomise();
}
public static void main(String[] args) {
MethodMaker methodMaker = new MethodMaker(){
#Override
public void randomise() {
// DO SOMETHING
}
};
methodMaker.randomise();
}
}
You have to put method outside main ,, and just call it in main ,, like this :-
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
Randomises();
}
public void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}

Categories