Hello I am a little confused. I had always thought that a class can have objects from other clases as attributes. My teacher recently told me that this is not true... so I am confused now: if one of the attributes of my class is a String, would that be an object inside my class???? Teacher says it's simply a variable...

the class itself is not a member of that other class, but an instance of it is.
not entirely sure what your question is, or what your teacher said, but sure, if you create a class
MyClassA, you can have another class with a MyClassA as either instance or static member.

I don't understand what point your teacher is trying to make.
The Java Language Spec doesn't use the term "attribute" except in two places. One is a very technical discussion of garbage collection, and the other uses it to mean a variable that can be accessed via get/set methods. So strictly speaking your teacher is in some private world of his own, using his own meaning for terms that don't have a proper definition. How does he define attribute so it's different from a variable?
Normally speaking, however, people use "attribute" to mean an instance data member of a class ( a "field" in JLS speak), so a class can have objects from other clases as attributes, it happens all the time, and your String is a perfect example.

Even so, the teacher is also correct when he says that a field is simply a variable. After all, in most practical ways a field is like a local variable that happens to have a longer life and larger scope. Perhaps the teacher is trying to make a clear distinction between a reference and an object.

Most people are in no danger of confusing references and objects so it is common to use the name of a field or variable as if it were the name of an object, but at various times a single variable can refer to various objects, and an object can be refered to by many distinct variables. I imagine some students who are new to programming languages and who are using Java as their first language might not find that distinction clear. And then there is C which uses the same notation that Java uses to declare a variable, but has a quite different meaning where the variable really is an object. As a result, a teacher might say that an attribute is not an object.

if one of the attributes of my class is a String, would that be an object inside my class???? Teacher says it's simply a variable...

There is no String inside your object. Keeping aside primitives for the time being, if a class is composed of multiple fields having different reference types (anything which derives from an Object), those names are nothing but references or pointers. There is no actual data being stored in your object. This distinction is important because you have the same object referred from multiple objects. If you think that a field is inside some other object, you might mistakenly think that each object has a copy of the field data which isn't true.

@JamesCherrill: My teacher means "variables can't be references to objects from other classes". Which,as you and everybody else says, is not really true.

@~s.o.s~: Thank you very much for your anwer too. It's extremely clear, especially the part about mistakenly thinking that objects have copies of the field data.

Thank you everyone else. All the explanations were very clarifying. I decided to rather study by myself, since the same teacher today said that a main method doesn't require a void return type, or any return type at all. The Java compiler disagrees...

the same teacher today said that a main method doesn't require a void return type, or any return type at all

Oh man, you really have a problem there; I hope he's not marking your homework. Anyway, you've discovered DaniWeb, where we may not always be right, but if we're wrong someone will point it out very quickly!

the same teacher today said that a main method doesn't require a void return type, or any return type at all. The Java compiler disagrees...

If your teacher said that about Java [1], then (s)he is plain wrong as per JLS 12.1.4

The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.

This method can be declared as either
public static void main(String[] args)
or
public static void main(String... args)

The only thing you are allowed to change in these two forms of main is the identifier of the array parameter.

[1] Are you sure that your teacher was talking about the Java language and not making a comparison with C# where both void and int are valid return types for the Main method?

@ JamesCherrill: He IS grading my homework. I'm gonna have to choose between pass the course and learn properly... He had us make a calculator (similar to the project for beginners on this site) but he has a weird conception of objects and classess... and fields (attributes for him)...

@mvmalderen: Yes, he was explicitly talking about Java.

Thank you all guys again. It is clear now regardless of what my teacher says.

said that a main method doesn't require a void return type

there he is correct. after all, he said "a main method", not thé main method.
with no return type, he might have meant void, and that you can decide on your own...

public static void main(String[] args){

}

public int main(){

}

don't see why that should be wrong? the first one doesn't have a return type (if that's his way to describe void - meaning - returning nothing), and the second one doesn't have void as returntype, yet they're both "main" methods.

commented: You make a good point about "A main method" versus "THE main method(s)". +0

When in doubt refer to the JLS. Here's what is says: (my bolding)

MethodDeclaration: MethodHeader MethodBody

MethodHeader: MethodModifiersopt TypeParametersopt Result MethodDeclarator Throwsopt
...
8.4.5. Method Return Type
The result of a method declaration either declares the type of value that the method returns (the return type), or uses the keyword void to indicate that the method does not return a value.

I don't see how that could be any clearer; a return type is mandatory, it may be the keyword void.
So IMHO the "teacher" is wrong about methods called "main" in general, and even more wrong about the main method. Scary!

commented: Yaay, JLS! :) +13

LOL. Yup. I asked him directly and he was talking about THE main method. He didn't like it when I showed him a Java program with a non-existing return type and an compiler complaining about it.

a non-existing returntype isn't the same as no return type. but they both cause the compiler to throw an exception.

Thanks again everybody. Like I said in another post. I just got a passing grade and forgot about the course. This teacher sucks so I continued to study by myself. The last java-related thing he said is that Java is a purely compiled language and that interpreted/scripting languages are inferior and should be taught or used. I really wonder what kind of engineers will this University produce with such teachers in it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.