i don't understand how class works

and constructor..

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1257 Views - Last Post: 08 January 2013 - 08:33 PM Rate Topic: -----

#1 xdeathcorex  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-March 10

i don't understand how class works

Posted 30 July 2010 - 07:45 AM

So here's my code. It's about a program that creates new bank account every time a user input their info.

public class BankAccount_main
{
	public static void main(String[] args)
    {
        BankAccount_class user;
        user = new BankAccount_class("my name here", 2000);  // IDE gave error here. idk why
    }
	
	class BankAccount_class
    {
        String Name = null;
        int accNo;
        int lastAssignNo = 1000;
        double accBalance = 0;
        final static double percentDividend = 4.0;
        double deposit;
        double withdrawal;
    
        public BankAccount_class(String a, double d)
        {
            //initialize instance variables
            Name = a;
            accNo = lastAssignNo + 1;
            accBalance = accBalance + d;
        }
    
        public BankAccount_class()
        {
            //initialize instance variables
            Name = null;
            accNo = lastAssignNo++;
        }
    
        public double Deposit(double a)
        {
            accBalance = accBalance + a;
            return accBalance;
        }
        
        public double Withdrawal(double a)
        {
            if (a >= accBalance)
            {
                System.out.println("You hace no sufficient balance");
                return accBalance;
            }
            else
            {
                accBalance = accBalance - a;
                return accBalance;
            }
        }
     }
}


so basically, i just want to make sure the input is working. that's why i only made the 'Name' and 'accBalance' parts.
so how can i make this work?

also, can someone explain to me why is there constructor? (why can't we just make method?)
i can't understand how 'static' works lol.
and what's the difference if i put public or private to a variable (or does it called 'instance'?) in a class?

phew. i'm a C++ leaver btw. and it seems Java is more difficult to understand for me. :P

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: i don't understand how class works

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: i don't understand how class works

Posted 30 July 2010 - 08:04 AM

I'm guessing the error is something about non-static elements being accessed from a static context. In which case, why do you need an inner class? I would just end the BankAccount_main class before starting your second class.

Quote

also, can someone explain to me why is there constructor? (why can't we just make method?)

The constructor is used to instantiate the Object. It is a method, but you cannot use other methods because the JVM recognizes constructors and creates Objects in memory when it sees them.

As for the rest of your questions, they are probably better answered in some of the tutorials in the Java Tutorials Section. You should check them out. :)
Was This Post Helpful? 0
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: i don't understand how class works

Posted 30 July 2010 - 08:06 AM

static refers to class, a static method is a class method, a static variable is a class variable as in they belong to that particular class

Take a look here:
http://download-llnw.../classvars.html

Public variables are accessable outside and inside of the class whereas private is accessible exclusively to that class alone. Protected is accessible to all subclasses of that class as well as itself.

We have constructors so that you can pass along information required. An object can hold information about particular things, making it more structured in accessing that information.

You are receiving errors because your main method is static, i.e. belongs only to that class.

This post has been edited by bcranger: 30 July 2010 - 08:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: i don't understand how class works

Posted 30 July 2010 - 08:09 AM

Quote

We have constructors so that you can pass along information required. An object can hold information about particular things, making it more structured in accessing that information. Methods cannot hold data.

Not entirely true. The JVM provides a null constructor that you can't pass any information to. Yes, you can use it to initialize variables and pass data. However, it's purpose is for instantiating the Object.

Also, methods can contain data locally. The scope is for the duration of the method, and that information cannot be accessed from within the method, beyond what it returns. However, they can contain information.
Was This Post Helpful? 1
  • +
  • -

#5 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: i don't understand how class works

Posted 30 July 2010 - 08:11 AM

Because BankAccount_class is an inner class inside the Student class you can not get a instance directly from inside of main method, you need first to have an instance of Student and from this the instance of BankAccount_class. Like this:

new Student().new BankAccount_class("my name here", 2000);



The main method is static that means that it can be accessed without you have to create any object from the class Student. So java compiler finds this method and start the execution of the program, but at this time doesn't exist any Student object so because of this you must create it.

A private variable means that a variable can not be accesed outside of the class while a public variable can be accessed for other objects outside of the class.
You need uderstand how works POO.

This post has been edited by guidojavier: 30 July 2010 - 08:21 AM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: i don't understand how class works

Posted 30 July 2010 - 08:42 AM

You mean OOP, right? :)
Was This Post Helpful? 0
  • +
  • -

#7 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: i don't understand how class works

Posted 30 July 2010 - 08:44 AM

Yes, I'm sorry I meant OOP...
Was This Post Helpful? 0
  • +
  • -

#8 xdeathcorex  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-March 10

Re: i don't understand how class works

Posted 30 July 2010 - 12:57 PM

Oook. thanks for the answers for my questions.

at this point,

View Postguidojavier, on 30 July 2010 - 07:11 AM, said:

Because BankAccount_class is an inner class inside the Student class you can not get a instance directly from inside of main method, you need first to have an instance of Student and from this the instance of BankAccount_class. Like this:

new Student().new BankAccount_class("my name here", 2000);



The main method is static that means that it can be accessed without you have to create any object from the class Student. So java compiler finds this method and start the execution of the program, but at this time doesn't exist any Student object so because of this you must create it.

A private variable means that a variable can not be accesed outside of the class while a public variable can be accessed for other objects outside of the class.
You need uderstand how works POO.


you mean create a new Student()? where do i have to put it? damn i'm a big noob at this.

second, you asked me to remove the 'static' in main, so i removed it. it doesn't gave any errors but compiler can't find any main. pfft.

the reason i made an inner class is because i am a beginner java learner so i think i just have to made it inside just to make sure i understand how it worked.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: i don't understand how class works

Posted 30 July 2010 - 01:02 PM

The public static void main(String[] args) method header is the only method from which a Java program can be run.

Quote

the reason i made an inner class is because i am a beginner java learner so i think i just have to made it inside just to make sure i understand how it worked.

Inner classes are more advanced. If you have a good handle on OOP, then that's one thing. However, if you are new to Java, don't mess with inner classes, as you are asking for trouble if you don't understand variable scope. Stick with outer classes only for the moment, and that should alleviate some or all of these issues.
Was This Post Helpful? 0
  • +
  • -

#10 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: i don't understand how class works

Posted 30 July 2010 - 01:03 PM

No I did not ask you to remove the 'static' in main. I was just telling you how it works. You must creat a new Student in the main method like this:

public static void main(String[] args)
{
    BankAccount_class user;
    user = new Student().new BankAccount_class("my name here", 2000);  
}


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,868
  • Joined: 27-December 08

Re: i don't understand how class works

Posted 30 July 2010 - 01:12 PM

A few months back, we had a discussion on Inner Classes in the Java Programmers Discussion forum. It covers the uses of inner classes quite well. I don't believe this instance is appropriate for inner classes.

Link: http://www.dreaminco...nd-their-usage/
Was This Post Helpful? 1
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i don't understand how class works

Posted 30 July 2010 - 02:02 PM

Keep it simple for the moment and forget the inner class

public class BankAccount_class
{
	String Name = null;
	int accNo;
	int lastAssignNo = 1000;
	double accBalance = 0;
	final static double percentDividend = 4.0;
	double deposit;
	double withdrawal;


	public BankAccount_class(String a, double d)
	{
		//initialize instance variables
		Name = a;
		accNo = lastAssignNo + 1;
		accBalance = accBalance + d;
	}

	public BankAccount_class()
	{
		//initialize instance variables
		Name = null;
		accNo = lastAssignNo++;
	}

	public double Deposit(double a)
	{
		accBalance = accBalance + a;
		return accBalance;
	}

	public double Withdrawal(double a)
	{
		if (a >= accBalance)
		{
			System.out.println("You hace no sufficient balance");
			return accBalance;
		}
		else
		{
			accBalance = accBalance - a;
			return accBalance;
		}
	}

	public static void main(String[] args)
	{
		BankAccount_class user;
		user = new BankAccount_class("my name here", 2000);  // IDE gave error here. idk why
	}


}


Was This Post Helpful? 2
  • +
  • -

#13 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: i don't understand how class works

Posted 30 July 2010 - 02:14 PM

well like pbl said better keep it simple. But now that I was watching the code again I was wondering where I came out with the Student class.. ???? anyway
what I meant was:

public static void main(String[] args)
{
    BankAccount_class user;	
    user = new BankAccount_main().new BankAccount_class("my name here", 2000);  // IDE gave error here. idk why
}


Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i don't understand how class works

Posted 30 July 2010 - 02:20 PM

View Postguidojavier, on 30 July 2010 - 03:14 PM, said:

well like pbl said better keep it simple. But now that I was watching the code again I was wondering where I came out with the Student class.. ???? anyway
what I meant was:

public static void main(String[] args)
{
    BankAccount_class user;	
    user = new BankAccount_main().new BankAccount_class("my name here", 2000);  // IDE gave error here. idk why
}



You probably meant BankAccount_main
Was This Post Helpful? 0
  • +
  • -

#15 xdeathcorex  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-March 10

Re: i don't understand how class works

Posted 31 July 2010 - 06:15 AM

Hey thanks pbl. it worked now.

for the inner class thing, it's clearly how newbie i am in this java thing lol.

i have another question. can i use two different public classes in a java file?

for example,

public class ClassName1{
.
.
.
}

public class ClassName2{
.
.
.
}

??
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2