encapsulating a constructor into regular method

All, I was wondering if it is possible to make a method that contains a constructor inside of the method. My constructor is this.

public StudentAPW(String name,int first,int second,int third )

{

System.out.println("Name: " + name);

System.out.println("First Test Score: " + first);

System.out.println("Second Test Score: " + second);

System.out.println("Third Test Score: " + third);

System.out.println("Student One:\n");

StudentAPW studentOne =new StudentAPW(Bob, 67, 72, 60);

System.out.println("\n");

System.out.println("Student Two:\n");

StudentAPW studentTwo =new StudentAPW(Billy, 84, 76, 89);

System.out.println("\n");

System.out.println("Student Three:\n");

StudentAPW studentThree =new StudentAPW(Joe, 100, 99, 100);

System.out.println("\n");

System.out.println("Student Four:\n");

StudentAPW studentFour =new StudentAPW(Ann, 96, 94, 99);

System.out.println("\n");

System.out.println("Student Five:\n");

StudentAPW studentFive =new StudentAPW(Sarah, 100, 100, 100);

System.out.println("\n");

System.out.println("Student Six:\n");

StudentAPW studentSix =new StudentAPW(Lou, 30, 28, 40);

System.out.println("\n");

if (score >=90)

grade ='A';

elseif (score >=80)

grade ='B';

elseif (score >=70)

grade ='C';

elseif (score >=60)

grade ='D';

else

grade ='F';

System.out.println("Score = " + score);

System.out.println("Grade = " + grade);

So what I hope to do is create a method

public static void grades()

that contains that entire constructor above without running in to any compile issues. Is this possible, because when I try to do this, the comipler recognizes that making a method that contains a constructor (1) is an illegal start of expression with the constructor

and (2) the constructor name does not match the

public class gradesDemo

so it then requires a return type for the constructor

Are there any ways to manipulate them to make this a possibility?

Any help is greatly appreciated.

Thanks

that the constructo name does not match the class name

[3482 byte] By [beatjunkiea] at [2007-11-27 11:02:15]
# 1

No, you cannot create a constructor inside a method or a method inside a constructor.

prometheuzza at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 2

what about calling on a constructor from another class?

beatjunkiea at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 3

> what about calling on a constructor from another

> class?

An example would be

in another class that is using a switch statement for menu options,

case 3:

StudentAPW.StudentAPW();

break;

I get a compiler error saying that it cant find the method, however, isn't a constructor a method? If so why can't I call on it from another class?

beatjunkiea at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 4

> > what about calling on a constructor from another

> > class?

>

> An example would be

>

> in another class that is using a switch statement for

> menu options,

>

> case 3:

>

> StudentAPW.StudentAPW();

>

> break;

>

> I get a compiler error saying that it cant find the

> method, however, isn't a constructor a method? If so

> why can't I call on it from another class?

You can, but not like that.

Do it like this:

StudentAPW sapw = new StudentAPW(/* the necessary parameters here */);

prometheuzza at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 5

> No, you cannot create a constructor inside a method

> or a method inside a constructor.

public class SomeClassOrOther {

public SomeClassOrOther() {

Object object = new Object() {

public void methodWithConstructorInIt() {

class WithConstructorInMethod {

WithConstructorInMethod() {

// do stuff

}

};

}

};

}

}

Although I'm obviously cheating :-) When it comes to actual classbytes, inner classes aren't actually contained within their enclosing type

georgemca at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 6

> > > what about calling on a constructor from another

> > > class?

> >

> > An example would be

> >

> > in another class that is using a switch statement

> for

> > menu options,

> >

> > case 3:

> >

> > StudentAPW.StudentAPW();

> >

> > break;

> >

> > I get a compiler error saying that it cant find

> the

> > method, however, isn't a constructor a method? If

> so

> > why can't I call on it from another class?

>

> You can, but not like that.

> Do it like this:

> StudentAPW sapw = new StudentAPW(/* the

> necessary parameters here */);

where in the code that I first used would I place that statement, and would it incorporate that entire constructor?

beatjunkiea at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 7

> > what about calling on a constructor from another

> > class?

>

> An example would be

>

> in another class that is using a switch statement for

> menu options,

>

> >

> case 3:

>

> StudentAPW.StudentAPW();

>

> break;

>

>

>

> I get a compiler error saying that it cant find the

> method, however, isn't a constructor a method? If so

> why can't I call on it from another class?

A constructor may be a method, depending on when you ask. As far as bytecode is concerned, they are, but that's about it. You can call it from another class. When you use the new keyword, you're calling a constructor

georgemca at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 8

@OP: But you are correct that you should instantiate those StudentAPW classes in another class, NOT inside the constructor of the class itself! You will get an infinite loop that way.

If you have further questions, please provide a SSCCE* because I find it hard to understand what it is you're trying to so.

Thanks.

* http://www.physci.org/codes/sscce.html

prometheuzza at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 9

> where in the code that I first used would I place

> that statement, and would it incorporate that entire

> constructor?

In another class. See reply #8.

prometheuzza at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 10

Hi Beatjunkie,

There're few changes I've done to your code. I have two classes. One is the main method class to where all the input/output will take place and the other one is your constructor class StudentAPW, which contains, checking scores, printing, taking values for parameters.

Note: if you want to check grade for each subject and if you have more than 10 students, I really prefer to use arrays. But it's up to you. you can check and do a tutorial in Sun how to do arrays.

So I hope you can understand this code. If anything just post here.

Good Luck.

/*

* StudentAPW.java

*

* Created on July 19, 2007, 4:22 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/**

*

* @author Trainee

*/

public class StudentAPW {

/*Please declare and define variables here.*/

String name;

int first;

int second;

int third;

int sum;

double average;

String grade;

/** Creates a new instance of StudentAPW */

public StudentAPW() {

}

/** Creates a new instance of StudentAPW with parameters*/

public StudentAPW(String name, int first, int second, int third) {

this.name = name;

this.first = first;

this.second = second;

this.third = third;

}

public void gradeStudent()

{

System.out.println("Name: " + name);

System.out.println("First Test Score: " + first);

System.out.println("Second Test Score: " + second);

System.out.println("Third Test Score: " + third);

System.out.println("\n");

sum = first + second + third; //I've replaced your score by the sum of all three scores

average = sum/3;

if (average >=90)

grade = "A";

else if (average >=80)

grade = "B";

else if (average >=70)

grade = "C";

else if (average >=60)

grade = "D";

else

grade = "E";

System.out.println("Total Score = " + sum);

System.out.println("Average = " + average);

System.out.println("Grade = " + grade);

}

}

/*

* Main.java

*

* Created on July 19, 2007, 4:21 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/**

*

* @author Trainee

*/

public class Main {

/** Creates a new instance of Main */

public Main() {

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

StudentAPW Student1 = new StudentAPW("beatJunkie", 85,80,95);

StudentAPW studentOne = new StudentAPW("Bob", 67, 72, 60);

StudentAPW studentTwo = new StudentAPW("Billy", 84, 76, 89);

StudentAPW studentThree = new StudentAPW("Joe", 100, 99, 100);

StudentAPW studentFour = new StudentAPW("Ann", 96, 94, 99);

StudentAPW studentFive = new StudentAPW("Sarah", 100, 100, 100);

StudentAPW studentSix = new StudentAPW("Lou", 30, 28, 40);

Student1.gradeStudent(); // you can do the same for all the other students as you want to display

}

}

ArchiEnger.711a at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 11

Looks like we have another Duke ***** here.

How does it feel to sell yourself for something that doesn't mean anything?

Do you also walk the street taking marbles in exchange?

Then again, maybe that's all it's worth.

masijade.a at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 12

> Looks like we have another Duke ***** here.

>

> How does it feel to sell yourself for something that

> doesn't mean anything?

>

> Do you also walk the street taking marbles in

> exchange?

>

> Then again, maybe that's all it's worth.

Don't be so hasty! I know they say you can't take it with you, but they'll have egg on their face when they turn up at Teh Pearly Gatez and find there's an admission fee. And we'll be screwed when it turns out you need dukes to get in.....

georgemca at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 13

Hello... Good Day to you masijade,

Do I care what you say?

No I don't. This forum shares and helps people.

If I know something, if I've got time, if I want to post something I will post to whoever I don't care.

People don't simply come to a forum and ask questions. If they want to learn they will learn. Else the path will be shorter.

If the fellow wants to give dukes to me, it's up to him or her.

Keep on trying and keep on going your way!

Kiasu!

ArchiEnger.711a at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 14

ArchiEnger,

We are all very impressed with your coding abilities. You are surely the next Einstein of Java, but please consider this:

If you've ever been Yosemite National Park in California you'll have to agree that it is one the most beautiful spots on earth. If you do go there, you'll see signs posted eveywhere: "do not feed the bears". And there are good reasons for this, not just that if you get close enough they could tear your limbs off. No, if you feed them, they don't learn to feed themselves. They learn to be lazy. They learn to beg for food.

Just like the bears at at Yosemite, you shouldn't spoon-feed the newbies. It teaches them to be lazy. It only motivates them not to think for themselves and even prevents them from learning how to think. Because of this, most of us here believe that spoon-feeding != teaching. So kindly follow this rule or fck off.

petes1234a at 2007-7-29 12:42:05 > top of Java-index,Java Essentials,New To Java...
# 15

> A constructor may be a method, depending on

> when you ask.

It's definitely *not* a method, as far as the JLS is concerned.

~

yawmarka at 2007-7-29 12:42:10 > top of Java-index,Java Essentials,New To Java...
# 16

> > A constructor may be a method, depending

> on

> > when you ask.

>

When you ask? Does that mean between 12 noon and 1 pm it is a method but all other times it is a constructor?

floundera at 2007-7-29 12:42:10 > top of Java-index,Java Essentials,New To Java...
# 17

> When you ask? Does that mean between 12 noon and 1 pm

> it is a method but all other times it is a constructor?

Dogpiling around here is de rigueur.

:o)

~

yawmarka at 2007-7-29 12:42:10 > top of Java-index,Java Essentials,New To Java...
# 18

I never miss a chance to kick a man while he is down.

floundera at 2007-7-29 12:42:10 > top of Java-index,Java Essentials,New To Java...
# 19

> [A constructor is] definitely *not* a method, as far as the JLS is concerned.

Supporting docs, just in case someone asks:

8.1.6 Class Body and Member Declarations

A class body may contain declarations of members of the class, that is, fields (8.3), classes (8.5), interfaces (8.5) and methods (8.4).

8.2 Class Members

Constructors, static initializers, and instance initializers are not members and therefore are not inherited.

All methods are members. No constructor is a member. Therefore, no constructor is a method.

~

yawmarka at 2007-7-29 12:42:10 > top of Java-index,Java Essentials,New To Java...
# 20

hello petes1234,

Thank you very much for your reply. Yes I know there's a rule. If I see a person has been in the forum and have never been able to post one good post, but beg here and there over the time, then I would never want to feed those ones.

But look at this, if this new fellow really wants to learn this, and if everyone else starts just giving hints, and the fellow doesn't even understand the hints, do you prefer the "newbie" to go and get lost?

I would help, but not blindly. I rather help first time and give guide lines. If you can read my post with the code, I've mentioned that for better enhancement and optimization, "newbie" can always go and do the tutorials.

I'm not from States. I'm from Asia. I've been to the worst sides of it where we don't have a single help, nor internet nothing. Only during my bachelor's degree I've got the chance to register in Sun Forums.

Yeah,may be the person I gave the code, might be from one of the lazy places with all facilities. But I didn't care about it, what I thought, just give a hand for the first time, if interested can go and surf and learn by your self and good at this.

null

ArchiEnger.711a at 2007-7-29 12:42:11 > top of Java-index,Java Essentials,New To Java...
# 21

> hello petes1234,

>

> Thank you very much for your reply. Yes I know

> there's a rule. If I see a person has been in the

> forum and have never been able to post one good post,

> but beg here and there over the time, then I would

> never want to feed those ones.

>

> But look at this, if this new fellow really wants to

> learn this, and if everyone else starts just giving

> hints, and the fellow doesn't even understand the

> hints, do you prefer the "newbie" to go and get

> lost?

>

> I would help, but not blindly. I rather help first

> time and give guide lines. If you can read my post

> with the code, I've mentioned that for better

> enhancement and optimization, "newbie" can always go

> and do the tutorials.

>

> I'm not from States. I'm from Asia. I've been to the

> worst sides of it where we don't have a single help,

> nor internet nothing. Only during my bachelor's

> degree I've got the chance to register in Sun Forums.

>

>

> Yeah,may be the person I gave the code, might be from

> one of the lazy places with all facilities. But I

> didn't care about it, what I thought, just give a

> hand for the first time, if interested can go and

> surf and learn by your self and good at this.

>

I don't totally disagree, it's just some of the newbies then keep coming back to the trough to feed and it sets up a vicious cycle. For the most part I try to give them pieces of code or outlines of pseudocode, but not whole apps.

petes1234a at 2007-7-29 12:42:11 > top of Java-index,Java Essentials,New To Java...
# 22

> > A constructor may be a method, depending

> on

> > when you ask.

>

> It's definitely *not* a method, as far as the JLS is

> concerned.

>

> ~

I know. As far as bytecode is concerned, it is. As I said ;-)

georgemca at 2007-7-29 12:42:11 > top of Java-index,Java Essentials,New To Java...
# 23

Hey petes1234,

don't totally disagree, it's just some of the newbies then keep coming back to the trough to feed and it sets up a vicious cycle. For the most part I try to give them pieces of code or outlines of pseudocode, but not whole apps.

=) I do agree with you for what you said last. I guess I better follow the way of posting portions of code when as keep them on the track, as in let them have the curiosity to try and debug by them selves and come up with the rest.

Thank you. It's good to meet you.

ArchiEnger.711a at 2007-7-29 12:42:11 > top of Java-index,Java Essentials,New To Java...
# 24

> > > A constructor may be a method,

> depending

> > on

> > > when you ask.

> >

> When you ask? Does that mean between 12 noon and 1 pm

> it is a method but all other times it is a

> constructor?

Nope. If you ask at a time when the JLS Militia are in earshot, it's not :-)

georgemca at 2007-7-29 12:42:11 > top of Java-index,Java Essentials,New To Java...