Aoi
Aoi

Reputation: 847

in java, when do I get access to this instance if not in main()?

So I am trying to make a simple SWING app for learning purposes, and I am a bit confused about this. I understand that the entry-point of the program is the main() function, and the object doesn't exist at that point so I can't refer to it with this, but then how and when do I know that the instance has been created?

My class extends JFrame so it already has a visual presence when I execute it, and right after it is created, I want to position it on a specific position on screen. But the only option seems to be to use new ClassName(), but that will create a second instance instead....so yeh, I am a total beginner and am confused

Upvotes: 0

Views: 63

Answers (3)

tommy knocker
tommy knocker

Reputation: 371

Before you start looking in GUI development with SWING, it is best to learn the basics of Java, Object Oriented Programming and instantiation.

Java is designed to be an object oriented language for example

    public class Animal{ //this is a super class
    private String name;

    public String getName(){
     return name;
    }
    public void setName(String name){
     this.name = name;
    }
    }

    public class Dog extends Animal{/*this is a subclass that inherits from Animal namely the name attribute, the getName and setName(String n) function.*/

    public Dog(String n){/*this is a constructor function/method, while classes would have a constructor method, you can create other constructor methods to accept parameters/required values */
     this.name = n;/*this name is equal to the String value inputted on instantiation.
if you happen to set a name inside the superclass (Animal) and want to use that, you can remove the String n from the constructor's parentensis and simply use super.getName(); instead of this.name;*/
    }
    }



    public class MainClass{

        public static void main(String args[]){
         Dog germanShepard = new Dog("Sir Isaac Woofington"); /*<-this is making an instance of a Dog.*/
        System.out.println(germanShepard.getName()); //this is printing out the name of the dog

    }
    }

to further add clarity on the main function.

public static void main(Strings args[]){
}

is mandatory to successfully compile an application, when you hit compile the compiler will looks for this method, if its not found it won't do anything. Further more it IS the entry point to run the rest of the code take the dog example the following is the sequence on processing: COMPILE

 Search for main method
found main method
sequentially go through the main method line by line 
Create an instance of dog called germanShepard
step into dog class 
dog class inherits Animal behaviour
set the dogs name to the property inherited from Animal
step out of class

print contents by accessing the method attached to dog called getName();

Upvotes: 1

Nathaniel Mallet
Nathaniel Mallet

Reputation: 401

You have to explicitly create the instance yourself. For example,

class MyJavaClass{
    public static void main(String args[]){
        MyJavaClass myJavaClass = new MyJavaClass();
    }
}

Upvotes: 1

assylias
assylias

Reputation: 328598

You just need to create an instance of your class in main:

MyClass c = new MyClass("A nice frame title here");

Then in your class constructor or methods:

public MyClass(String title) {
   //you can use this here
}

public void method() {
   //and here
}

Upvotes: 1

Related Questions