Java moving Main class reference - java

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){
}
}

Related

How does this basic hello world statement come as an error?

class helloworld{
public static void main(String[] args){
speaker speakerObject = new speaker(); //this becomes an error!!!
speakerObject.greet();
}
public class speaker {
public void greet(){
System.out.println("Hello World!");
}
}
}
when i try to make an object, it becomes an error!
im using VS
You're referencing a non-static variable in a static context, and you have an error in the inner class declaration, basically it should be:
public void main(String[] args){
speaker speakerObject = new speaker();
speakerObject.greet();
}
class speaker {
public void greet(){
System.out.println("Hello World!");
}
}
It's an error of No enclosing instance of type __YourClassName__ is accessible.
Try to understand the error, speaker is an inner class of helloworld, which is by definition is associated with helloworld class. So declaring it as a static class, it won't need any instance of helloworld class.
And one more thing, If you are new to java, cultivate a habit of using first letter capital in your class name, It will be strongly appreciated if you use upper camel case for naming conventions of class.
Below is the corrected code:
class helloworld{
public static void main(String[] args){
speaker speakerObject = new speaker(); //this becomes an error!!!
speakerObject.greet();
}
public static class speaker {
public void greet(){
System.out.println("Hello World!");
}
}
}

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);
}
}

java - can a class type parameter from a static main class be passed to another class

I have 2 classes the static main class and class B. I'm trying to pass main to B, where there is a method that sets fields.
Can this be done?
If so, could you please provide examples?
public static void main(String[] args) {
ArrayList a = new ArrayList()
class b = new class()
b.update(b);
}
class a {
public void update(ArrayList a) {
//updates the encapsulated arrayList field
}
}
The error message keeps on saying that one is static and the other is non-static, but they should be pointing the same object
I'm not entirely sure what you are trying to do, but here is an example that shows that you can pass an instance of the main class into another class:
public class A {
private String str = null;
public static void main(String[] args) {
A a = new A();
B b = new B(a);
System.out.println(a.getStr());
}
public String getStr() {
return this.str;
}
public void setStr(String str) {
this.str = str;
}
}
public class B {
public B(A a) {
a.setA("hello");
}
}
Running this code will print out hello.
main is static and public, so you can call it from any other class as any other public static method: statically.
if you have a class A that contains a
public static void main(String[] args)
method, then class B can call this method like
A.main(s);
where s is String[]
your question is far from clear, so I suggest you to add more code samples to make it clear what you're really trying to do.

Simple Java OOP Class Error

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();
}
}

Categories